Debian Codegraph @

doc/manual/manual.tex

raw
1 \documentclass[a4paper]{memoir}
2 \usepackage{manual}
3 \setlength{\parindent}{0.0cm}
4 \setlength{\parskip}{1.4ex}
5 \headstyles{default}
6 %%% ToC down to susubsections
7 \settocdepth{subsubsection}
8 %%% Numbering down to sections
9 \setsecnumdepth{subsection}
10 %%% index
11 \makeindex[lines]
12 %%%% end preamble -----------------------------------------------------
13
14 \begin{document}
15 \tightlists
16 \midsloppy
17 \chapterstyle{ell}
18 % ----- Title Page
19 \frontmatter
20 \pagestyle{empty}
21 %%
22 \title{\cafeobj~User's Manual \\ --- ver.1.5.6 ---}
23 \author{Ataru T. Nakagawa \and Toshimi Sawada \and Kokichi Futatsugi
24 \and Norbert Preining}
25 %\date{\today}
26 \date{2016-3-4}
27 \bibliographystyle{plain}
28
29 \maketitle
30 \vfill
31 \begin{center}
32 \includegraphics[scale=1]{cafeobj-logo.png}
33 \end{center}
34 \vfill
35 %% \Hline
36 %% \begin{center}
37 %% \footnotesize{Revised on 2012/3 by Kosakusha}
38 %% \end{center}
39 \thispagestyle{empty}
40 %% \Hline
41 %% %%%%
42 %% \subsubsection*{NOTE}
43 %% This manual is out of date and is subject to change.
44
45 %% \subsubsection*{Changes from Version 1.3}
46
47 %% The main changes from version 1.3 are as follows.
48 %% \begin{itemize}
49 %% \item In addition to Gnu Common Lisp, CMU Common Lisp and Allegro Common Lisp
50 %% can be used as a platform.
51 %% \item Faster rewrite engines are now available, and can be invoked from
52 %% \cafeobj.
53 %% \item The predicate \verb|==>| (for transition relations) gets a more
54 %% powerful support. A couple of new switches are added for this reason.
55 %% \item Behavioural axioms can be used in equational reduction, and
56 %% an operator attribute \kbd{coherent} is added for this purpose.
57 %% \item A behavioural reduction command is introduced.
58 %% \item Sort predicates are introduced.
59 %% \end{itemize}
60 %% \Hline
61 \clearpage
62 \newpage
63 \pagestyle{headings}
64 \tableofcontents
65 %\listoffigures
66 \clearpage
67
68 %%%%%%%%%%%%%%%%%%%%%%
69 \pagestyle{ruled}
70 \chapter{Introduction}
71
72 \cafeobj~is a specification language based on three-way extensions to
73 many-sorted equational logic: the underlying logic is
74 order-sorted, not just many-sorted; it admits unidiretional transitions,
75 as well as equations; it also accommodates hidden sorts, on top of
76 ordinary, visible sorts. A subset of \cafeobj~is executable, where the
77 operational semantics is given by a conditional order-sorted term rewriting
78 system. These theoretical bases are indispensable to employ \cafeobj~properly.
79 Fortunately, there is an ample literature on these subjects, and we are able
80 to refer the reader to, e.g., \cite{e-m85}, \cite{m-g82} (for basics of
81 algebraic specifications), \cite{osa}, \cite{osa-survey} (for order-sorted
82 logic), \cite{hsa} (for hidden sorts), \cite{eatcs-coalg} (for coinduction),
83 \cite{rew-logic} (for rewriting
84 logic), \cite{institution} (for institutions), and \cite{trs-eatcs},
85 \cite{trs-handbook} (for term rewriting systems), as primers.
86 The logical aspects of \cafeobj~are explained in detail in \cite{razvan96-1}
87 and \cite{cafeobj-rep}. This manual is for the initiated, and we sometimes
88 abandon the theoretical rigour for the sake of intuitiveness.
89
90 For a very brief introduction, we just highlight a couple of features
91 of \cafeobj. \cafeobj~is an offspring of the family of algebraic
92 specification techniques. A specification is a text, usually of
93 formal syntax. It denotes an algebraic system constructed out of
94 sorts (or data types) and sorted (or typed) operators. The system
95 is characterised by the axioms in the specification. An axiom was
96 traditionally a plain equation (``essentially algebraic''), but is now
97 construed much more broadly. For example, \cafeobj~accommodates
98 conditional equations, directed transitions, and (limited) use of
99 disequality.
100
101 The underlying logic of \cafeobj~is as follows\footnote{
102 Some of the authors feel that some of these technical terms are
103 queer. But to avoid confusion, we basically keep to the
104 established terminology.
105 }.
106 \begin{itemize}
107 \item[]\textbf{ Order-sorted logic}\cite{osa}. A sort may be a subset of
108 another sort. For example, natural numbers may be embedded into rationals.
109 This embedding makes valid the assertion that 3 equals 6/2. It also
110 realises ``operator inheritance'', in the sense that an operator
111 declared on rationals are automatically declared on natural numbers.
112 Moreover, the subsort relation offers you a simple way to define
113 partial operations and exception handling.
114 \item[] \textbf{ Rewriting logic}\cite{rew-logic}. In addition to equality,
115 which is subject to the law of symmetry, you may use transition relations,
116 which are directed in one way only. State transitions are
117 naturally formalised by those relations. In particular, transition
118 relations are useful to represent concurrency and/or indeterminacy.
119 \item[]\textbf{ Hidden sorts}\cite{hsa}. You have two kinds of equivalence. One
120 is a minimal equivalence, that identifies terms (elements) iff
121 they are the same under the given equational theory. Another
122 equivalence, employed for so-called hidden sorts, is behavioural:
123 two terms are equivalent iff they behave identically under the
124 given set of observations.
125 \end{itemize}
126 We would also like to emphasise a very useful feature of \cafeobj.
127 \begin{itemize}
128 \item[]\textbf{ Parameters}. There are many sorts that are inherently
129 generic. Stacks, lists, sets and so on have operations that
130 act independently of the properties of base (``data'') elements.
131 A more tricky case is priority queues, which require base elements to
132 have an order relation. You may define these sorts by
133 parameterised modules, where base elements are parameterised out.
134 A parameter may be subject to constraints. For example, the parameter
135 of a priority queue module may be declared an ordered set, not
136 an arbitrary set.
137 \end{itemize}
138
139 \section{Legends}
140
141 Restrictions, particulars, and prejudices of the current implementation
142 shall be highlighted by preceding \emph{!!}, as
143
144 \begin{warning}
145 Woe betide those who do not heed this warning!
146 \end{warning}
147
148 while syntactical definitions are shown as
149
150 \begin{bsyntax} title
151 \mbox{\texttt{I am a language construct.}}
152 \end{bsyntax}
153 \setcounter{bsyntax}{0}
154
155 The definitions inside the box are by way of notations as follows.
156 \begin{itemize}
157 \item[(1)] $\{$, $\}$ are meta-syntactical brackets.
158 \item[(2)] $|$ separates alternatives.
159 \item[(3)] $*$ means zero or more repetitions of the preceding construct.
160 \item[(4)] $+$ means one or more repetitions.
161 \item[(5)] $*_{,}$ means zero or more repetitions separated by commas.
162 \item[(6)] $+_{,}$ means one or more repetitions separated by commas.
163 \item[(7)] $[$, $]$ enclose optional constructs.
164 \item[(8)] The above symbols, to appear as they are, are enclosed by quotes,
165 as ``\verb|{|''.
166 \item[(9)] Symbols in the typewriter font appear as they are: \kbd{x} is
167 literally \kbd{x}. In case this convention is ambiguous, e.g. when a
168 parenthesis appears in isolation, symbols are quoted as in (8).
169 \end{itemize}
170 For example,
171
172 \begin{tabular}{l}
173 \kbd{A} $+_,$
174 \end{tabular}
175
176 represents sequences of one or more \verb|A|'s, separated by commas.
177
178 %%%%%%%
179 \mainmatter
180 \pagestyle{ruled}
181 \chapter{Dialogue with the System}
182
183 \section*{Availability}
184
185 You may obtain a complete distribution package from the
186 following site.
187 \begin{vvtm}
188 \begin{ccode}
189 http://www.cafeobj.org/
190 \end{ccode}
191 \end{vvtm}
192 % where \verb|???| is a version numbering. Or you can get a system linked
193 % from the site
194 % \begin{vvtm}
195 % \begin{verbatim}
196 % http://www.ldl.jaist.ac.jp/cafeobj/
197 % \end{verbatim}
198 % \end{vvtm}
199 % Mirror sites in Europe and USA are under consideration.
200
201 % The current version works under Unix operating systems (usual
202 % vernaculars, including various versions of Sun OS, FreeBSD, and Linux, are
203 % all okay) or Mac (MacOSX 10.5 or later), and is implemented on
204 % various Common Lisp processors (including
205 % Gnu, CMU, Allegro, and MacLisp). But you cannot run, for example, MacLisp
206 % on Solaris, for an obvious reason. On the question of which Lisp systems
207 % to use on which operating systems, and which Lisp systems to be obtained
208 % from where, consult the installation guide (README) in the distribution
209 % package.
210 The current version works under i386 and am64 Linux, i386 and amd64
211 MacOSX (version 10.5 or later), and win32. Binary package of these
212 platforms can be obtained from the above site. You can also download
213 source code of the system from the same place.
214
215 % In principle the system should also work on Windows with Allegro, but there
216 % is no guarantee of its behaviours. The implementors have no intention
217 % of testing the system under Windows environments.
218
219 \section{Hello, Goodbye}\label{sec:p2-startup}
220
221 \subsection{Starting Up}
222
223 After installation, type ``\verb|cafeobj|'' to your shell to
224 invoke the system. Then the \cafeobj~interpreter starts up, producing
225 a welcoming message.
226 % \begin{vvtm}
227 % \begin{verbatim}
228 % % cafeobj
229 % -- loading standard prelude
230 % ; Loading /mnt1/lang/cafeobj-1.4/prelude/std.bin.
231 %
232 % -- cafeobj system Version 1.4.100203p116 --
233 % built: 1984 Dec 24 Mon 3:44:17 GMT
234 % prelude file: std.bin
235 % ***
236 % 1999 Dec 31 Fri 23:59:45 GMT
237 % Type ? for help
238 %
239 %
240 % CafeOBJ>
241 % \end{verbatim}
242 % \end{vvtm}
243 \begin{vvtm}
244 \begin{ccode}
245 % cafeobj
246 -- loading standard prelude
247
248 -- CafeOBJ system Version 1.5.6(PigNose0.99,b3) --
249 built: 2016 Jan 20 Wed 13:27:45 GMT
250 prelude file: std.bin
251 ***
252 2016 Feb 29 Mon 1:22:30 GMT
253 Type ? for help
254 ***
255 -- Containing PigNose Extensions --
256 ---
257 built on SBCL
258 1.3.1
259 CafeOBJ>
260 \end{ccode}
261 \end{vvtm}
262 For a moment, ignore the message.
263 ``\verb|CafeOBJ>|'' is a prompt for your requests. You may type
264 modules, views, parsing instructions, reduction commands, and so on,
265 each of which shall be gradually introduced in the rest of the manual.
266
267 As a first experiment, try feeding a trivial module definition
268 (you need not worry about the meaning for now).
269 \begin{vvtm}
270 \begin{ccode}
271 module TEST { [ Elt ] }
272
273 -- defining module! TRUTH
274 -- reading in file : truth
275
276 -- done reading in file: truth
277 .............._...........* done.
278 -- defining module! BASE-BOOL..
279 -- reading in file : eql
280
281 processing input : /usr/local/share/cafeobj-1.5/lib/eql.cafe
282
283 -- defining module! EQL.._...* done.
284 -- done reading in file: eql
285 ........._* done.
286 processing input : /usr/local/share/cafeobj-1.5/lib/sys_bool.cafe
287
288 -- defining module! BOOL....._......................* done.._* done.
289 CafeOBJ>
290 \end{ccode}
291 \end{vvtm}
292 Your input is up to the first newline character. The rest is from the
293 system. It shows that the system has accepted the definition, put
294 this module somewhere in its memory, to be available hereafter.
295 %%% t.swd 2010/June
296 \footnote{As you see in the example above, system may load some other
297 modules required for defining your module.
298 In this case, \kbd{TRUTH}, \kbd{BOOL} are loaded in automatic.
299 These are modules which provides built-in Boolean types and
300 operations.
301 More on this at page~\pageref{sec:p2-import-decl}.}
302 %%%
303 You may continue to feed other modules, or check if indeed the module
304 is correctly stored. As a further trial, type
305 \begin{vvtm}
306 \begin{ccode}
307 CafeOBJ> select TEST
308 TEST>
309 \end{ccode}
310 \end{vvtm}
311 The command \texttt{select}\index{\texttt{select}} takes as argument a module name.
312 ``\verb|TEST>|'' is a new prompt. As you see, \verb|TEST| is the name
313 of the module just \texttt{select}ed. The system maintains
314 a module, called the \emph{current module}\index{current module},
315 that is the focus of manipulation at the time. The initial prompt
316 ``\verb|CafeOBJ>|'' indicates the current module is not yet supplied.
317 Or a module \verb|cafeobj| is now current (it is possible, if confusing,
318 to define such a module).
319 Various commands, such as parsing and reduction, are executed under
320 the current module. For more details on current modules, see Section
321 \ref{sec:p2-current-module}.
322
323 %\begin{warning}
324 % \cafeobj~has a command structure quite similar to that of
325 % OBJ3\cite{OBJ}. In fact, former versions of \cafeobj~accepted
326 % a subset of OBJ3's commands. This version, however, contains
327 % some major extensions, and adopts different conventions. In OBJ3,
328 % the current module changes whenever you have defined a new module,
329 % fed a reduction command, etc. In contrast, in this version of \cafeobj~,
330 % the current module does not change (by default). To get a convention
331 % similar to OBJ3, you may toggle a switch (cf. Section
332 % \ref{sec:p2-current-module}).
333 %\end{warning}
334
335 %\begin{warning}
336 % This version has discarded another convention of OBJ3. In OBJ3, many
337 % commands and language constructs require ``blank-period'' stoppers.
338 % Former versions of \cafeobj~clinged to the convention, partly
339 % for compatibility, partly for easier lexical analyses.
340 % This version does not require them, most of the time.
341 %\end{warning}
342
343 \subsection{Quitting the System}
344
345 To quit \cafeobj, type \verb|quit| (or \verb|q| for short) or the end of
346 input character (usually control-D) at a prompt.
347 Then the interpreter terminates and the control is given back to
348 the original shell.
349 \begin{vvtm}
350 \begin{ccode}
351 CafeOBJ> quit
352 [Leaving CafeOBJ]
353 %
354 \end{ccode}
355 \end{vvtm}
356 %\begin{warning}
357 %Former versions of \cafeobj~allowed you to quit to the underlying lisp
358 %environment. Upon \verb|quit|, this version goes straight back to the
359 %shell.
360 %\end{warning}
361
362 \subsection{Emergency --- Resumption and Bug Reports}
363
364 In detecting errors, \cafeobj~growls.
365 \begin{vvtm}
366 \begin{ccode}
367 CafeOBJ> module ERROR }
368 [Error]: was expecting the symbol `{' not `}' .
369
370 CafeOBJ>
371 \end{ccode}
372 \end{vvtm}
373 The above message starting with ``\verb|[Error]|'' indicates that
374 the system detected a syntax error in processing your input.
375
376 Due to irrecoverable input errors, or just to mulfunctions, \cafeobj
377 ~may stop abnormally, and throw the control away to an error environment.
378 \begin{vvtm}
379 \begin{ccode}
380 CafeOBJ> ^C
381
382 debugger invoked on a SB-SYS:INTERACTIVE-INTERRUPT in thread ...
383
384 Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
385
386 restarts (invokable by number or by possibly-abbreviated name):
387 0: [CONTINUE] Return from SB-UNIX:SIGINT.
388 1: [ABORT ] Return to CafeOBJ Top level.
389 2: Exit CafeOBJ.
390
391 ("bogus stack frame")
392 0]
393 \end{ccode}
394 \end{vvtm}
395 The above session was obtained by typing the interrupt code (usually
396 Control-C) at the initial prompt, forcing \cafeobj~to stop willy-nilly.
397 The above prompt indicates you plunged into an errror
398 environment. The above message may look familiar to you, if you are
399 a user of SBCL/Allegro Lisp. The message was from the underlying
400 Common Lisp interpreter, and the error environment is in this case the
401 SBCL error loop.%\footnote{
402 %Error loops of CMU, Allegro, and MacLisp work more or less similarly,
403 %and the same recover command \kbd{:q} works.
404 %}.
405
406 Following the instructions from the debugger, you may resume the
407 \cafeobj~session at the very point where you left of by
408 selecting~\verb|0|, as one can see in the following example where the
409 interrupt key was pressed in the middle of a module definition:
410 \begin{vvtm}
411 \begin{ccode}
412 CafeOBJ> mod ERROR {
413
414 debugger invoked on a SB-SYS:INTERACTIVE-INTERRUPT in thread ...
415
416 Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
417
418 restarts (invokable by number or by possibly-abbreviated name):
419 0: [CONTINUE] Return from SB-UNIX:SIGINT.
420 1: [ABORT ] Return to CafeOBJ Top level.
421 2: Exit CafeOBJ.
422
423 ("bogus stack frame")
424 0] 0
425 }
426
427 -- defining module ERROR_* done.
428
429 CafeOBJ>
430 0] 1
431 CafeOBJ>
432 \end{ccode}
433 \end{vvtm}
434
435 Selecting~\verb|1| instead will terminate the current input and return
436 to the top-level, which would result in the module \verb|ERROR| above
437 remaining undefined.
438
439 There may be irrecoverable cases, when \verb|0| (or similar commands of
440 other Common Lisp systems) fails to work.
441 Then try ``\verb|(cafeobj t)|''. This is a more
442 robust means to return to \cafeobj. If, for goodness, you are unable
443 to return to \cafeobj~even then, exit the debugger (control-D normally
444 works) and start from scratch.
445
446 If the system still works fine, but if you happen to have destroyed
447 some vital module definitions, try
448 \begin{vvtm}
449 \begin{ccode}
450 CafeOBJ> reset
451 \end{ccode}
452 \end{vvtm}
453 which restores the definitions of built-in modules (Chapter
454 \ref{sec:p2-builtin-module}) and preludes (Section \ref{sec:p2-init-opt}).
455 This command does not affect other modules. For complete cleanup, use
456 \begin{vvtm}
457 \begin{ccode}
458 CafeOBJ> full-reset
459 \end{ccode}
460 \end{vvtm}
461 which reinitialises the internal state of the system. But before calling
462 this command, remember that
463 you will lose all the module definitions you have supplied so far.
464 You are warned.
465
466 \paragraph{Bug Reports}
467
468 The system is under continual improvement. If you detect any bugs,
469 malfunctions, or just inconveniences, do let us know. The support team is
470 reachable via
471 \begin{vvtm}
472 \begin{ccode}
473 [email protected]
474 \end{ccode}
475 \end{vvtm}
476 We also accept bugs at our bug tracker
477 \begin{vvtm}
478 \begin{ccode}
479 https://tracker.cafeobj.org/
480 \end{ccode}
481 \end{vvtm}
482
483
484 %%%%%%%%
485 \section{Files and Libraries}
486
487 \subsection{Reading Files}\label{sec:p2-reading-files}
488
489 Not every module is as trivial as \verb|TEST|, and you will % sooooon
490 soon need library systems for store/recovering module definitions,
491 test cases, and so on. \cafeobj~requires every imported module
492 (cf. Section
493 \ref{sec:p2-import-decl}) to be
494 defined beforehand, so you need a recursive file reference
495 mechanism. As a minimum, you need the following command.
496
497 \begin{bsyntax} \texttt{input} command\index{\texttt{input}}\index{\texttt{in}}\Hline
498 \texttt{input}~\textit{pathname}
499 \end{bsyntax}
500
501 \verb|input| (or \verb|in| for short)
502 requests the system to read the file specified by the
503 pathname. The file itself may contain \verb|input| command, and so on,
504 ad nauseum. \cafeobj~reads the file up to eof, or until it encounters
505 a line that only contains (the literal) \verb|eof|.
506
507 \begin{bsyntax} \texttt{eof}~command \index{\texttt{eof}}\Hline
508 \texttt{eof}
509 \end{bsyntax}
510
511 This little command is especially useful when the % program
512 codes are under development. You can supply the system with only those
513 definitions that are up to scratch.
514
515 Pathnames follow the conventions of the Unix file system. Both
516 relative and absolute paths are possible, and
517 ``\verb|~|''\index{\texttt{\~} (home directory)}
518 denotes your home directory.
519 If there is no file with the pathname, the system expands the name
520 with suffixes ``kbd{.cafe}''\footnote{The older systems prior
521 to version 1.4.8 used a different suffix ``\kbd{.mod}''.
522 This is also supported for compatibility.}
523 \index{\texttt{.cafe} (suffix)} \index{\texttt{.mod} (suffix)}
524 or ``\kbd{.bin}''
525 \index{\texttt{.bin} (suffix)} and re-search the
526 directory. By convention, a file suffixed with ``\kbd{.cafe}'' is assumed
527 to contain definitions and commands written in \cafeobj,
528 while ``\kbd{.bin}''
529 indicates a file containing internal codes. Normally, you should prepare
530 ``\kbd{.cafe}'' files yourself, and leave the system to create
531 ``\kbd{.bin}'' files (see Section \ref{sec:p2-save-restore}).
532
533 Some directory traversal commands of Unix are also available.
534
535 \begin{bsyntax} \texttt{cd}~ command \index{\texttt{cd}}\Hline
536 \texttt{cd}~\textit{pathname}
537 \end{bsyntax}
538
539 \begin{bsyntax} \texttt{pwd}~\textit{command}\index{\texttt{pwd}}\Hline
540 \texttt{pwd}
541 \end{bsyntax}
542
543 \begin{bsyntax} \texttt{ls} command \index{\texttt{ls}}\Hline
544 \texttt{ls} \textit{pathname}
545 \end{bsyntax}
546
547 where pathnames must be those of directories.
548
549 \begin{warning}
550 The above commands act like their namesakes in Unix shells,
551 but are not exactly the same.
552 The arguments to \verb|cd|, \verb|ls| are not optional, but necessary.
553 Also, the system does not recognise \verb|~bimbo| as the home directory of
554 \verb|bimbo|.
555 \end{warning}
556
557 \subsection{Saving and Restoring}\label{sec:p2-save-restore}
558
559 Module definitions may be saved in an internal format, for efficient
560 later retrieval. The commands
561
562 \begin{bsyntax} \texttt{save} command \index{\texttt{save}}\Hline
563 \texttt{save} \textit{pathname}
564 \end{bsyntax}
565
566 \begin{bsyntax} \texttt{restore} command \index{\texttt{restore}}\Hline
567 \texttt{restore} \textit{pathname}
568 \end{bsyntax}
569
570 save/restore module definitions into/from the designated file.
571 Pathnames must be those of files. To adhere to the system's convention,
572 file names should be suffixed with ``\verb|.bin|''. In place of
573 \verb|restore| command, you may also use \verb|input| command
574 (Section \ref{sec:p2-reading-files}).
575
576 \begin{warning}
577 \kbd{save} saves the contents of prelude files (see Section
578 \ref{sec:p2-init-opt})
579 as well as module definitions given during the session.
580 \kbd{restore} reestablishes those definitions as they are saved, and
581 the effects may be different from reading the file with \kbd{input} command.
582 It is safer to use \kbd{restore} command to read \kbd{save}d files.
583 \end{warning}
584
585 % NP 20151124 save-system is not available anymore
586
587 % You can also save the entire image of the system, including
588 % the state of module definitions, into a dump file, by the following
589 % command.
590
591 % \begin{bsyntax} \texttt{save-system} command \index{\texttt{save-system}}\Hline
592 % \texttt{save-system} \textit{pathname}
593 % \end{bsyntax}
594
595 % \begin{warning}
596 % \kbd{save-system} dumps the image of Common Lisp, so do not abuse
597 % this command.
598 % And due to the restictions of Common Lisp systems, this command may
599 % not work.
600 % \end{warning}
601
602 \subsection{Initialisations and Options}\label{sec:p2-init-opt}
603
604 Recall (Section \ref{sec:p2-startup}) that the initial system message
605 contained
606 \begin{vvtm}
607 \begin{ccode}
608 -- loading standard prelude
609 ...
610 prelude file: std.bin
611 \end{ccode}
612 \end{vvtm}
613 In starting up, \cafeobj~autoloads\index{autoloading} a set of modules,
614 called standard preludes.\index{prelude}\index{standard preludes} They
615 define frequently used data types
616 and house-keeping data (cf. Chapter \ref{sec:p2-builtin-module}).
617
618 You may initialise the system to your taste, with options and
619 initialisation files.\index{initialisation file}\index{\texttt{.cafeobj}}
620 If a file named ``\kbd{.cafeobj}'' is under your
621 home directory, the system reads the file as if you type
622 \begin{vvtm}
623 \begin{ccode}
624 CafeOBJ> in ~/.cafeobj
625 \end{ccode}
626 \end{vvtm}
627 after the startup. This initialisation is useful to change default
628 values of modes and switches (cf. \verb|set| command, Section
629 \ref{sec:p2-set-command}).
630
631 Various options\index{option (for startup)} control the initial state of
632 the system.
633 \begin{itemize}
634 \item[\kbd{-q}] This option prohibits reading \verb|.cafeobj|.
635 \item[\kbd{-p}] \emph{pathname}.~~ Instead of the standard prelude
636 file, which is set up during installation,
637 \index{standard prelude file}
638 the system reads the given pathname (which must be a file).
639 The suffix convention is applicable here.
640 \item[\kbd{+p}] {\em pathname}. ~~The sytstem reads the given pathname
641 {\em in addition to} the standard prelude file.
642 \item[\kbd{-l}] {\em pathname-list}. ~~In processing \kbd{input} command,
643 the system searches the pathname list to locate the specified file.
644 (For default search paths, see Section \ref{sec:p2-lib-system}.)
645 All the pathnames must be directories, and the list must be separated
646 by ``\kbd{:}''.
647 \item[\kbd{+l}] {\em pathname-list}. ~~Pathnames are {\em added} to
648 the search directory list.
649 \end{itemize}
650 For example,
651 \begin{vvtm}
652 \begin{ccode}
653 % cafeobj +p /usr/cern/particle/muon.bin
654 -l /usr/cern/lib/std:/usr/cern/lib/muon:/usr/home/eva/cafe
655 \end{ccode}
656 \end{vvtm}
657 makes the system (1) read the standard prelude file, (2) read
658 \kbd{muon.bin} in addition, (3) set the search directories as given,
659 and (4) read the initialisation file \kbd{.cafeobj}, if it exists.
660
661 %\begin{warning}
662 % Pathnames in \verb|-l|, \verb|+l| options must be absolute.
663 %\end{warning}
664
665 You may also make the system read files by giving them as arguments.
666 For example,
667 \begin{vvtm}
668 \begin{ccode}
669 % cafeobj lemma1 lemma2
670 \end{ccode}
671 \end{vvtm}
672 has the same effects as typing
673 \begin{vvtm}
674 \begin{ccode}
675 CafeOBJ> input lemma1
676 ... messages from the system ...
677 CafeOBJ> input lemma2
678 ... messages from the system ...
679 \end{ccode}
680 \end{vvtm}
681 at the beginning of the session.
682
683 Note that, even though ``\kbd{-p}'' options, file name arguments and
684 ``\kbd{.cafeobj}'' all cause the system to process definitions and
685 commands, their effects are slightly different. Modules defined in
686 prelude files, whether standard or user-supplied, are treated as
687 preludes. This difference reveals itself when, e.g., \kbd{reset}
688 command (Section \ref{sec:p2-startup}) is invoked.
689
690 Finally, if you are disinclined to remember all of the above,
691 just memorise the help option.
692 \begin{vvtm}
693 \begin{ccode}
694 % cafeobj -help
695 \end{ccode}
696 \end{vvtm}
697 lists the available options.
698
699 \subsection{Batch \cafeobj} \label{sec:p2-batch}
700
701 If you do not want to pay much attention to the system,
702 you may run the system non-interactively.
703 An option \kbd{-batch} causes the system to run on the batch mode,
704 and the system terminates immediately. This is useful in processing
705 a large file. For example,
706 \begin{vvtm}
707 \begin{ccode}
708 % cafeobj -batch goliath > cafe.out
709 \end{ccode}
710 \end{vvtm}
711 forces the system to read and process \kbd{goliath} (or \kbd{goliath.bin}
712 or \kbd{goliath.cafe} or \kbd{goliath.mod}) while you have an afternoon tea.
713
714 \subsection{Module Libraries} \label{sec:p2-lib-system}
715
716 As suggested in the previous section, \cafeobj~maintains a list of
717 search directories. Some of them are {\em standard}.
718 You may change the list by ``\kbd{-l}'' or ``\kbd{+l}'' options,
719 as explained in Section \ref{sec:p2-init-opt}. You may also change it
720 during the session with \verb|set| command (Section \ref{sec:p2-set-command}).
721 For example,
722 \begin{vvtm}
723 \begin{ccode}
724 CafeOBJ> set libpath + /usr/home/maria/cafe:/usr/home/tania/cafe
725 \end{ccode}
726 \end{vvtm}
727 adds the two directories to the search list, while
728 \begin{vvtm}
729 \begin{ccode}
730 CafeOBJ> set libpath /usr/home/maria/cafe:/usr/home/tania/cafe
731 \end{ccode}
732 \end{vvtm}
733 replaces the search list by the twosome.
734 Each pathname should be separated by ``\kbd{:}'', like in
735 ``\kbd{-l}'' and ``\kbd{+l}'' options.
736
737 By default, if an apparent single file name is given as argument to
738 \kbd{input} command, the system searches the current directory,
739 then standard library directories. The standard library directories
740 \index{standard library}
741 are fixed at the time of installation, and are relative to the binary
742 of the interpreter. On a Unix-like system with installation to
743 \verb|/usr/local| it would be
744 \begin{vvtm}
745 \begin{ccode}
746 /usr/local/share/cafeobj-1.5/lib
747 \end{ccode}
748 \end{vvtm}
749 If the search paths are modified, the sytem searches (a) the current
750 directory, (b) the paths given by the user (from left to right), and
751 (c) the above default paths (in case of \kbd{+} or \kbd{+l} options).
752
753 \begin{warning}
754 The current directory has a privileged status. It is always searched
755 first, and cannot be suppressed by ``\kbd{-l}'' options or \kbd{set}
756 command.
757 \end{warning}
758
759 To inspect the current search paths, type
760 \begin{vvtm}
761 \begin{ccode}
762 CafeOBJ> show libpath
763 \end{ccode}
764 \end{vvtm}
765 (see Section \ref{sec:p2-show-command} for \kbd{show} command in general).
766
767 \subsection{Requiring and Providing}
768
769 If you are familiar with emacs\index{emacs}, you may fancy the
770 following pair of commands for configuration management.
771
772 \begin{bsyntax} \texttt{require} command \index{\texttt{require}}\Hline
773 \texttt{require} \textit{feature} $[$\textit{pathname}$]$
774 \end{bsyntax}
775
776 \begin{bsyntax} \texttt{provide} command \index{\texttt{provide}}\Hline
777 \texttt{provide} \textit{feature}
778 \end{bsyntax}
779
780 \kbd{require} requires a feature, which usually
781 denotes a set of module definitions. Given this command, the
782 system searches for a file named the \textit{feature}, and read the file
783 if found. If a pathname is given, the system searches for a file
784 named the pathname instead. If the \textit{feature} contains double colons
785 (\verb|::|) then they are interpreted as directory separators, that
786 is, a call to \verb|require foo::bar| would search for a file
787 \verb|foo/bar.cafe| in the current \verb|libpath|.
788
789 \kbd{provide} discharges a feature
790 requirement: once \kbd{provide}d, all the subsequent
791 \kbd{require}ments of a feature are assumed to have been fulfilled
792 already.
793 For example, you may be
794 at the point of reading a file that refers to a module of
795 lists. Suppose your convention is to store a set of container modules
796 --- lists, bags, sets, and so on --- in a single file. Suppose further
797 that, for various
798 reasons, you have a couple of alternative definitions of those modules,
799 stored in files \kbd{container-1.cafe}, \kbd{container-2.cafe}, etc.
800 Then it is convenient to insert the command
801 \begin{vvtm}
802 \begin{ccode}
803 provide container
804 \end{ccode}
805 \end{vvtm}
806 in each file, so that, once one of them read in, the system knows
807 that a module of lists is already supplied.
808
809 To see which features have been provided, type
810 \begin{vvtm}
811 \begin{ccode}
812 CafeOBJ> show features
813 \end{ccode}
814 \end{vvtm}
815 (see Section \ref{sec:p2-show-command} for \kbd{show} command).
816 \index{\texttt{show}}
817
818 To continue the above example, provided there is a file
819 \kbd{container-1.cafe} that
820 defines lists, bags, et al. and contains the \kbd{provide} command line
821 as above, you may get the following session.
822 \begin{vvtm}
823 \begin{ccode}
824 CafeOBJ> provide numbers
825
826 CafeOBJ> require container container-1
827
828 processing input : ./container-1.cafe
829 -- defining module LIST_* done.
830 -- defining module BAG_* done.
831 -- defining module SET_* done.
832 CafeOBJ> require container container-1
833
834 CafeOBJ> show features
835 container numbers
836 \end{ccode}
837 \end{vvtm}
838 The first invocation of \kbd{require} made the system read the file,
839 while the second invocation caused nothing.
840 Also, since \kbd{numbers} was
841 explicitly \kbd{provide}d, the system presumes the feature as given.
842
843 \section{Some Helpful Commands}\label{sec:p2-other-toplevels}
844
845 \subsection{Help Command and Top-level Commands}\label{sec:p2-help-command}
846
847 Help commands of \cafeobj~in general employ ``?'', instead of
848 ``help'', ``aide-moi'', ``socorro'', and so on. You may remember that
849 the welcoming message contained the line
850 \begin{vvtm}
851 \begin{ccode}
852 Type ? for help
853 \end{ccode}
854 \end{vvtm}
855 saying that the topmost help command is
856
857 \begin{bsyntax} command listing command \texttt{?}(help command)\index{\texttt{?} (help command)}\Hline
858 \texttt{?}
859 \end{bsyntax}
860 which prints out a online help guidance.
861 This is a good startng point for navigating the system.
862 \begin{vvtm}
863 \begin{ccode}
864 CafeOBJ> ?
865 You are at top level, no context module is set.
866
867 ** Here are commands for CafeOBJ online help system.
868 '?com [<class>]' Shows available commands classified by <class>,
869 ommiting <classy> shows a list of <class>.
870 '? <name>' Gives the reference manual description of <name>
871 '?ex <name>' Similar to '? <name>', but in this case
872 shows examples if available.
873 '?ap <term> [<term>] ...' Searches all available online docs for the terms
874 passed. Type '? ?ap' for more detailed descriptions.
875 ** Typing 'com' will show the list of major toplevel commands.
876 ** URL 'http://cafeobj.org' provides anything you want to know about CafeOBJ.
877 \end{ccode}
878 \end{vvtm}
879
880 Typing \texttt{com} will show majour tope level commands avaiable:
881 \begin{vvtm}
882 \begin{ccode}
883 CafeOBJ> com
884 -- CafeOBJ major top level commands:
885 NOTE: Top level definitional forms include declaration of `module' and ...
886 -- help:
887 ? shows the current context and the brief guide for ...
888 -- exit:
889 quit -or- q exit from CafeOBJ interpreter.
890 -- setting working context:
891 select <Modexp> . set the module specified by a module expression ...
892 open <Modexp> . open the module specified by a module expression ...
893 <Modexp> will be a current module.
894 close close the opening module.
895 -- term rewriting commands:
896 reduce -or-
897 red [in <Modexp> : ] <term> .
898 rewrite <term> using equations as rewerite rules
899 optional <Modexp> specifies the rewriting context ...
900 exec [in <Modexp> : ] <term> .
901 rewrite <term> using both equations and rules
902 optional <Modexp> specifies the rewriting context ...
903 -- term parser:
904 parse [in <Modexp> : ] <term> .
905 parse <term>, print out the result
906 optional <Modexp> specfies the parsing context ...
907 -- inspection:
908 show -or- describe print various info., for further help, type `show ?'
909 -- setting switches:
910 set set toplevel switches, for further help: type `set ?'
911 protect <Modexp> prevent module <Modexp> from redefinition
912 unprotect <Modexp> un-set protection of module <Modexp>
913 -- reading in files:
914 input -or-
915 in <pathname> requests the system to read the file specified by ...
916 The file itself may contain 'input' commands.
917 -- system intialization
918 reset restores the definitions of built-in modules and ...
919 but does not affect other modules.
920 full reset reinitializes the internal state of the system.
921 all supplied modules definitions are lost.
922 -- misc. commands
923 cd <directory> change the system's working directory
924 ls <directory> list files in directory
925 pwd print the current directory
926 ! <command> fork shell <command> (Unix only)
927 \end{ccode}
928 \end{vvtm}
929
930 All available commands may be classified as follows.
931 \begin{itemize}
932 \item[-] Starting module declarations. \kbd{module}
933 (Section \ref{sec:p2-module-decl-syntax}), \kbd{view}
934 (Section \ref{sec:p2-view}), and some others introduce building
935 blocks of \cafeobj~codes. They are to be explained in detail, in the rest of
936 the manual.
937 \item[-] Checking properties of modules. \kbd{check}
938 (Section \ref{sec:p2-check-command}).
939 \item[-] Managing systems. \verb|quit| (Section \ref{sec:p2-startup}),
940 \verb|select| (Section \ref{sec:p2-current-module}),
941 \verb|set| (Section \ref{sec:p2-set-command}),
942 \verb|protect| (Section \ref{sec:p2-module-decl-syntax}), and others.
943 \item[-] Managing files. \verb|input| (Section \ref{sec:p2-reading-files}),
944 \verb|save| (Section \ref{sec:p2-save-restore}), and others. They
945 should be already familiar to you.
946 \item[-] Peeping systems. \verb|show| and \verb|describe|
947 (Section \ref{sec:p2-show-command}) are at your command.
948 \item[-] Evaluating terms. \verb|reduce|,
949 \verb|execute|, and some others. Part of
950 \verb|set| command also concerns evaluation. They are all explained in
951 Chapter \ref{sec:p2-term-evaluation}.
952 \item[-] Helping theorem proving. \verb|apply|, \verb|open|, et al. They
953 shall be explained in Chapter \ref{sec:p2-theorem-proving}.
954 \end{itemize}
955
956 Many commands have abbreviations. We list them below (For a complete
957 list of abbreviations, see Appendix \ref{sec:syntax-abbrev}).
958 \begin{center}
959 \begin{tabular}{ll||ll}
960 \verb|module| & \verb|mod| & \verb|module!| & \verb|mod!| \\
961 \verb|module*| & \verb|mod*| & \verb|quit| & \verb|q| \\
962 \verb|show| & \verb|sh| & \verb|describe| & \verb|desc| \\
963 \verb|input| & \verb|in| & \verb|reduce| & \verb|red| \\
964 \verb|execute| & \verb|exec| & \verb|b-reduce| & \verb\bred| \\
965 \end{tabular}
966 \end{center}
967
968 \begin{warning}
969 Other abbreviations may be accepted by the system. But unless
970 explicitly stated in the manual, correct behaviours are not
971 guaranteed.
972 \end{warning}
973
974 Some of the commands takes many different kinds of arguments. It is
975 unlikely for you to remember all. In such cases, the system
976 interprets the argument ``\verb|?|'' as your desperate call. For
977 example,
978 \begin{vvtm}
979 \begin{ccode}
980 CafeOBJ> set ?
981 \end{ccode}
982 \end{vvtm}
983 prints a list of arguments acceptable to \verb|set| command, to be introduced
984 immediately.
985
986 \subsection{Switch-Toggling Command}\label{sec:p2-set-command}
987
988 The system maintains various kind of switches. By definition a
989 {\em switch}\index{switch} is two-valued (\verb|on|\index{\texttt{on}}
990 and \verb|off|\index{\texttt{off}}). The following command changes
991 the value of a switch.
992
993 \begin{bsyntax} \texttt{set} command for switches \index{\texttt{set}}\Hline
994 \texttt{set} \textit{switch\_name} $\{$ \texttt{on} $|$ \texttt{off} $\}$
995 \end{bsyntax}
996
997 As explained in Section \ref{sec:p2-help-command},
998 you get a list of available switches by just typing
999 \begin{vvtm}
1000 \begin{ccode}
1001 CafeOBJ> set ?
1002 \end{ccode}
1003 \end{vvtm}
1004 and the current value of switches are listed by
1005 \begin{vvtm}
1006 \begin{ccode}
1007 CafeOBJ> show switches
1008 \end{ccode}
1009 \end{vvtm}
1010 (for \verb|show|\index{\texttt{show}} command,
1011 see Section \ref{sec:p2-show-command}).
1012
1013 A switch that affects the general behaviour of the system is
1014 \verb|verbose|. After this switch is turned \verb|on|, the system suddenly
1015 becomes talkative in responding to many commands, such as \verb|show| and
1016 \verb|describe|.
1017 Another such switch is \verb|quiet|. If this switch is \verb|on|,
1018 the system does not bother you with messages other than blatant error messages.
1019 Other switches are relevant to the specific commands of the system,
1020 and will be explained as the need arises.
1021
1022 \begin{warning}
1023 Available switches are subject to frequent changes.
1024 \end{warning}
1025
1026 Some arguments to \verb|set| command are not switches, and may range
1027 over many values. An example is \verb|libpath|, which was appeared
1028 in Section \ref{sec:p2-lib-system}. The general syntax might be
1029
1030 \begin{bsyntax} \texttt{set} command for others \index{\texttt{set}} \Hline
1031 \texttt{set} \textit{a\_name} $[$\textit{option}$]$ \textit{value}
1032 \end{bsyntax}
1033
1034 but options and values are dependent on the name, and names have no
1035 common characteristic other than that they refer to something.
1036 We shall explain them individually, as the occasions arise.
1037
1038 \subsection{Inspection Commands}\label{sec:p2-show-command}
1039
1040 \verb|show| command prints information on modules, system status, et al.
1041 The acceptable arguments are many, so you are wise to remember that
1042 \begin{vvtm}
1043 \begin{ccode}
1044 CafeOBJ> show ?
1045 \end{ccode}
1046 \end{vvtm}
1047 lists them all (cf. Section \ref{sec:p2-help-command}).
1048
1049 The general syntax is complicated. It is better understood
1050 when divided into two. For the first half, you have
1051
1052 \begin{bsyntax} \texttt{show} command for module inspection\Hline
1053 \index{\texttt{show}}
1054 \texttt{show} $[$\textit{module\_expression}$]$
1055 \texttt{show} $[$\texttt{all}$]$ $\{$ \texttt{sorts} $|$ \texttt{ops} $|$ \texttt{sign} $|$ \texttt{axioms} $\}$ $[$\textit{module\_expression}$]$
1056 \texttt{show} $\{$ \texttt{vars} $|$ \texttt{params} $|$ \texttt{subs} $\}$ $[$\textit{module\_expression}$]$
1057 \texttt{show} $\{$ \texttt{sort} \textit{sort\_name} $\}$ $|$ $\{$ \texttt{op} \textit{operator\_name} $\}$ $[$ \textit{module\_expression}$]$
1058 \texttt{show} $\{$ \texttt{sub} \textit{number} $\}$ $|$ $\{$ \texttt{param} \textit{parameter\_name} $\}$ $|$ $\{$ $[$ \texttt{all}$]$ \texttt{rules} $\}$
1059 ~~~~~~~~$[$\textit{module\_expression}$]$
1060 \texttt{show view} \textit{view\_name}
1061 \end{bsyntax}
1062
1063 Do not bother to memorise all of these for now --- or ever.
1064 We illustrate them after module building constructs are
1065 explained (cf. Chapter \ref{sec:p2-inspect-module}). For the second half,
1066 you have
1067
1068 \begin{bsyntax} \texttt{show} command for other inspection \Hline
1069 \texttt{show tree} $|$ $\{$ \texttt{term} $[$\textit{let\_variable}$]$ $[$\texttt{tree}$]$ $\}$ $|$ $\{$ \texttt{subterm} $[$\texttt{tree}$]$ $\}$
1070 \texttt{show $[$ let $|$ selection $|$ pending $|$ context $|$ modules $|$ views $|$}
1071 ~~~~~~~~\texttt{time $|$ limit $|$ stop $|$ features $|$ memo}
1072 \texttt{show $[$all$]$ $\{$ rule \textit{rule\_specification} $\}$ $|$ $\{$ switches $[$\textit{switch\_name}$]$ $\}$}
1073 \end{bsyntax}
1074
1075 Again, do not bother now. Most of these arguments are unrelated, and
1076 we explain the system's response separately for each of them,
1077 in relevant chapters.
1078
1079 There is a command that enables
1080 you to look deeper into module definitions.
1081
1082 \begin{bsyntax} \texttt{describe} command \index{\texttt{describe}} \Hline
1083 \texttt{describe $[$\textit{module\_expression}$]$}
1084 \texttt{describe $[$all$]$ $\{$ sorts $|$ ops $|$ sign $|$ axioms $\}$ $[$\textit{module\_expression}$]$}
1085 \texttt{describe $\{$ vars $|$ params $|$ subs $\}$ $[$\textit{} module\_expression $]$}
1086 \texttt{describe $\{$ sort \textit{sort\_name} $\}$ $|$ $\{$ op \textit{operator\_name} $\}$ $[$\textit{module\_expression}$]$}
1087 \end{bsyntax}
1088
1089 \begin{warning}
1090 In using the system, you may notice that it treats
1091 \verb|show| and \verb|describe| in combination. In fact
1092 \begin{vvtm}
1093 \begin{ccode}
1094 CafeOBJ> show ?
1095 \end{ccode}
1096 \end{vvtm}
1097 lets you know the syntax of \verb|describe| as well as \verb|show|.
1098 Try also
1099 \begin{vvtm}
1100 \begin{ccode}
1101 CafeOBJ> describe ?
1102 \end{ccode}
1103 \end{vvtm}
1104 \end{warning}
1105
1106 \chapter{Module Declaration}\label{sec:p2-module-declaration}
1107
1108 The basic building blocks of \cafeobj~are modules. This chapter explains
1109 the outermost structure of a module.
1110
1111 \section{Overall Structure}\label{sec:p2-module-decl-syntax}
1112
1113 A module without parameters\index{module} has the following syntax.
1114 \index{\texttt{module}}
1115
1116 \begin{bsyntax} module without parameters \Hline
1117 \texttt{module} \textit{module\_name} ``\texttt{\{}''
1118 ~~\textit{module\_element} $*$
1119 ``\texttt{\}}''
1120 \end{bsyntax}
1121
1122 A module name\index{module name} is an arbitrary character string.
1123 A module element\index{module element} is either
1124 %% (1) import declaration, (2) sort declaration (with ordering),
1125 %% (3) operator declaration, (4) record declaration,
1126 %% (5) variable declaration, (6) equation declaration, or
1127 %% (7) transition declaration.
1128 (1) import declaration, (2) sort declaration (with ordering),
1129 (3) operator declaration, (4) variable declaration,
1130 (5) equation declaration, or (6) transition declaration.
1131 In the subsequent chapters, we shall introduce each of them in turn.
1132
1133 A general rule about module elements is that a referent must precede
1134 in the textual order. For example, you cannot refer to a variable unless
1135 you declared it somewhere above. Another general rule is that
1136 a module element belongs to the module where it appears, and,
1137 unless imported (Section \ref{sec:p2-import-decl}), it cannot be
1138 referred to in other modules.
1139
1140 Different modules with the same name may be declared, but only the latest
1141 declaration is maintained. You cannot recover previous declarations.
1142 The system gives a warning when a previous declaration is discarded.
1143
1144 Some modules are vital for the system to behave correctly. Those modules
1145 cannot be overwritten. There are other special modules, not so vital but
1146 worth a diligent protection.
1147 An example is a module that defines natural number
1148 arithmetic, called \verb|NAT| (cf. Chapter \ref{sec:p2-builtin-module}).
1149 You may remove the guard from those modules, by the command
1150
1151 \begin{bsyntax} \texttt{unprotect} command \index{\texttt{unprotect}} \Hline
1152 \texttt{unprotect} \textit{module\_name}
1153 \end{bsyntax}
1154
1155 For the reverse effect, you may use
1156
1157 \begin{bsyntax} \texttt{protect} command \index{\texttt{protect}} \Hline
1158 \texttt{protect} \textit{module\_name}
1159 \end{bsyntax}
1160
1161 which make it impossible to overwrite the given module. You may use
1162 this command to protect your own cherished modules.
1163 The following session illustrates the behaviour of the system.
1164 In this session, we only use the syntax introduced so far.
1165 \begin{vvtm}
1166 \begin{ccode}
1167 CafeOBJ> module TEST { }
1168
1169 -- defining module TEST_* done.
1170 CafeOBJ> module TEST { }
1171
1172 -- defining module TEST
1173 [Warning]: redefining module TEST _* done.
1174 CafeOBJ> protect TEST
1175
1176 CafeOBJ> module TEST { }
1177
1178 -- defining module TEST
1179 [Error]: module TEST is protected!
1180 CafeOBJ> unprotect TEST
1181
1182 CafeOBJ> module TEST { }
1183
1184 -- defining module TEST
1185 [Warning]: redefining module TEST _* done.
1186 CafeOBJ>
1187 \end{ccode}
1188 \end{vvtm}
1189 According to the syntax of module declarations, empty modules
1190 are allowed, as was the \verb|TEST| here.
1191 You are warned even if the unprotected \verb|TEST| is overwritten.
1192 Once under protection, \verb|TEST| cannot be redefined. Stripped
1193 of protection, it is subject to redefinition again.
1194
1195 \section{Blocks in a Module}\label{sec:p2-blocks-in-mod}
1196
1197 Module elements can be classified in three categories: (1) references
1198 to other modules, (2) signature definitions\index{signature},
1199 and (3) axioms.
1200 It is possible to gather elements in each category into blocks.
1201 \index{block (of declarations)}
1202 References to other modules may be clustered by the following syntax.
1203
1204 \begin{bsyntax} \texttt{imports} block \index{\texttt{imports}} \Hline
1205 \texttt{imports} ``\texttt{\{} \textit{module\_element} $*$ ``\texttt{\}}''
1206 \end{bsyntax}
1207
1208 where module elements are restricted to import declarations
1209 (Section \ref{sec:p2-import-decl}). Similarly, signature definitions
1210 and axioms may be clustered by
1211
1212 \begin{bsyntax} \texttt{signature} block \index{\texttt{signature}} \Hline
1213 \texttt{signature} ``\texttt{\{} \textit{module\_element} $*$ ``\texttt{\}}''
1214 \end{bsyntax}
1215
1216 \begin{bsyntax} \texttt{axioms} block \index{\texttt{axioms}} \Hline
1217 \texttt{axioms} ``\texttt{\{} \textit{module\_element} $*$ ``\texttt{\}}''
1218 \end{bsyntax}
1219
1220 %% where module elements in a \verb|signature| block
1221 %% are restricted to declarations of sorts,
1222 %% operators, and records (Chapter \ref{sec:p2-signature}),
1223 %% while those in an \verb|axioms| block are restricted
1224 %% to declarations of variables, equations, and transitions
1225 %% (Chapter \ref{sec:p2-axioms}).
1226 where module elements in a \verb|signature| block
1227 are restricted to declarations of sorts and operators (Chapter \ref{sec:p2-signature}),
1228 while those in an \verb|axioms| block are restricted
1229 to declarations of variables, equations, and transitions
1230 (Chapter \ref{sec:p2-axioms}).
1231
1232 \verb|imports|,
1233 \verb|signature|, and \verb|axioms| blocks may appear more than once in a
1234 single module. \verb|signature| may be abbreviated to \verb|sig|, and
1235 \verb|axioms| to \verb|axs|.\index{\texttt{sig}}\index{\texttt{axs}}
1236
1237 These block constructs are for the sake of
1238 legibility, and have no semantic connotation. For example,
1239 \label{exs:simple-nat}
1240 \begin{vvtm}
1241 \begin{ccode}
1242 module SIMPLE-NAT {
1243 signature {
1244 [ Zero NzNat < Nat ]
1245 op 0 : -> Zero
1246 op s : Nat -> NzNat
1247 op _+_ : Nat Nat -> Nat
1248 }
1249 axioms {
1250 vars N N' : Nat
1251 eq 0 + N = N .
1252 eq s(N) + N' = s(N + N') .
1253 }
1254 }
1255 \end{ccode}
1256 \end{vvtm}
1257 is exactly the same module declaration as
1258 \begin{vvtm}
1259 \begin{ccode}
1260 module SIMPLE-NAT {
1261 [ Zero NzNat < Nat ]
1262 op 0 : -> Zero
1263 op s : Nat -> NzNat
1264 op _+_ : Nat Nat -> Nat
1265 vars N N' : Nat
1266 eq 0 + N = N .
1267 eq N + s(N') = s(N + N') .
1268 }
1269 \end{ccode}
1270 \end{vvtm}
1271 or minutely blocked
1272 \begin{vvtm}
1273 \begin{ccode}
1274 module SIMPLE-NAT {
1275 signature {
1276 [ Zero NzNat < Nat ]
1277 }
1278 signature {
1279 op 0 : -> Zero
1280 op s : Nat -> NzNat
1281 }
1282 signature {
1283 op _+_ : Nat Nat -> Nat
1284 }
1285 axioms {
1286 vars N N' : Nat
1287 }
1288 axioms {
1289 eq 0 + N = N .
1290 eq N + s(N') = s(N + N') .
1291 }
1292 }
1293 \end{ccode}
1294 \end{vvtm}
1295 \begin{warning}
1296 In inspecting modules by \verb|show| command
1297 (Section \ref{sec:p2-show-command}),
1298 you may notice that the system uses these block constructs,
1299 even if you did not supply them (cf. Chapter \ref{sec:p2-inspect-module}).
1300 \end{warning}
1301
1302 \section{Tight Modules, Loose Modules}\label{sec:p2-obj-th}
1303
1304 In \cafeobj, a module plays two different r{\^ o}les, depending on where it
1305 appears or what it contains. In one guise, a module denotes a unique
1306 (up to iso) model ({\em tight denotation}).\index{tight denotation}
1307 In the other, it denotes a class of models ({\em loose denotation}).
1308 \index{loose denotation} The latter is the case when
1309 a module is used as parameter (see Section \ref{sec:p2-parameter});
1310 when it defines behavioural properties (Section \ref{sec:p2-hsort-beget-axs}),
1311 and so on. We do not go into the detail in this manual, but the distinction
1312 is very important. You should consult \cite{cafeobj-rep} or other
1313 technical materials at least once.
1314
1315 Sometimes it is necessary or convenient to restrict the r{\^ o}le of
1316 a module. Alternative module declarations below allow you to do that.
1317
1318 \begin{bsyntax} module for tight denotation \Hline
1319 \texttt{module!} \textit{module\_name} ``\texttt{\{}
1320 ~~~~\textit{module\_element} $*$
1321 ``\texttt{\}}''
1322 \end{bsyntax}
1323
1324 \begin{bsyntax} module for loose denotation \Hline
1325 \texttt{module*} \textit{module\_name} ``\texttt{\{}
1326 ~~~~\textit{module\_element} $*$
1327 ``\texttt{\}}''
1328 \end{bsyntax}
1329
1330 The syntax inside is the same as declarations that start
1331 with \verb|module|.
1332
1333 When a module is declared with ``\verb|module!|'', it always denotes a
1334 unique model. When declared with ``\verb|module*|'', it always denotes a
1335 class of models.
1336
1337 %\begin{warning}
1338 % There may be several syntactical criteria to decide which module
1339 % should or should not be a tight denotation or a loose denotation.
1340 % The current implementation is laissez-faire and
1341 % does not impose syntactic restrictions on ``\verb|module!|'' or
1342 % ``\verb|module*|''.
1343 %\end{warning}
1344
1345 \section{Comment}\label{sec:p2-comment-syntax}
1346
1347 A comment\index{comment} of \cafeobj~is one of the followings.
1348 \begin{itemize}
1349 \item[a.] From ``\texttt{**}'' to the end of line (eol).\index{\texttt{**}}
1350 \item[b.] From ``\texttt{**>}'' to the eol.\index{\texttt{**>}}
1351 \item[c.] From ``\texttt{--}'' to the eol.\index{\texttt{--}}
1352 \item[d.] From ``\texttt{-->}'' to the eol.\index{\texttt{-->}}
1353 \end{itemize}
1354 Comments may be inserted either between module declarations or
1355 between module elements.
1356
1357 A comment that starts with ``\texttt{**>}'' or ``\texttt{-->}''
1358 is displayed when it is input to the
1359 system. This facility is useful when a file is being read, or when
1360 you want to state the expected results of evaluations. For example,
1361 if you are excessively uneasy about what is going on, you may prepare
1362 a file that contains
1363 \begin{vvtm}
1364 \begin{ccode}
1365 **> immediately above TEST
1366 module TEST {
1367 --> just entered into TEST
1368 **> now sort declaration
1369 [ Elt ] -- this comment should be supressed
1370 **> sort declaration ended
1371 --> operator declaration starts
1372 op a : -> Elt -- this comment should be supressed
1373 --> operator declaration ended
1374 **> finishing TEST declarartion
1375 }
1376 --> finished!
1377 \end{ccode}
1378 \end{vvtm}
1379 and feed the file --- call it \kbd{test} here --- to the system.
1380 The result is as follows.
1381 \begin{vvtm}
1382 \begin{ccode}
1383 CafeOBJ> in test
1384
1385 processing input : ./test.cafe
1386 **> immediately above TEST
1387 -- defining module TEST
1388 -- just entered into TEST.
1389
1390 ** now sort declaration..
1391
1392 ** sort declaration ended.
1393
1394 -- operator declaration starts..
1395
1396 -- operator declaration ended.
1397
1398 ** finishing TEST declarartion._* done
1399
1400 --> finished!
1401 CafeOBJ>
1402 \end{ccode}
1403 \end{vvtm}
1404 As shown above, ``\kbd{>}'' is stripped when the comment appeared within
1405 a module declaration.
1406
1407 As an aside, you may have noticed a couple of
1408 ``\kbd{.}'''s in the system messages. These dots indicate that the
1409 system is running hard. More dots mean more labour.
1410 ``\kbd{\_}'' and ``\kbd{*}'' just before ``\kbd{done}''
1411 were also from the system.
1412
1413 \section{Import Declaration} \label{sec:p2-import-decl}
1414
1415 An import declaration\index{import declaration} is [sic] to import
1416 declarations from another module.
1417
1418 \begin{bsyntax} import declaration \Hline
1419 $\{$ \texttt{protecting $|$ extending $|$ using $|$ including} $\}$ ``('' \textit{module\_expression} ``)''
1420 \end{bsyntax}
1421
1422 \verb|protecting| etc. are called {\em importation mode\/}s,
1423 \index{importation mode} and indicate how to import the designated module.
1424 You may abbreviate the modes to \kbd{pr}, \kbd{ex}, \kbd{inc}, and \kbd{us}
1425 respectively.
1426 A module expression is a designation of a module, and is explained later
1427 (Section \ref{sec:p2-module-expression}).
1428 For the current purpose, it is enough to know that an individual module name
1429 is by itself a module expression.
1430
1431 To illustrate the meaning of import declarations, here is a contrived example.
1432 \begin{vvtm}
1433 \begin{ccode}
1434 module BARE-NAT {
1435 [ Nat ]
1436 op 0 : -> Nat
1437 op s : Nat -> Nat
1438 }
1439
1440 module BARE-NAT-AGAIN {
1441 protecting (BARE-NAT)
1442 }
1443 \end{ccode}
1444 \end{vvtm}
1445 The declaration of \kbd{BARE-NAT-AGAIN} is equivalent to
1446 \begin{vvtm}
1447 \begin{ccode}
1448 module BARE-NAT-AGAIN {
1449 [ Nat ]
1450 op 0 : -> Nat
1451 op s : Nat -> Nat
1452 }
1453 \end{ccode}
1454 \end{vvtm}
1455 i.e. it is a copy, a sad shadow, of \kbd{BARE-NAT}. Only the name is
1456 changed.
1457
1458 To explain the precise meaning of importation modes, we have to delve into
1459 a model-theoretic theatre, which is situated outside of this manual. So
1460 let us offer only a cursory glance here.
1461 The modes state to what extent the intended models of the imported modules
1462 are preserved within those of the importing modules. Intuitively,
1463 when \kbd{M'} is imported,
1464 \begin{itemize}
1465 \item[-] \texttt{protecting}\index{\texttt{protecting}} is the strongest
1466 commandment.
1467 This mode requires all the intended models of \kbd{M'} be preserved
1468 as they are.
1469 \item[-] \texttt{extending}\index{\texttt{extending}} allows the models of
1470 \kbd{M'} to be inflated, but does not allow them to be
1471 collapsed.
1472 \item[-] \texttt{including}\index{\texttt{including}} does not
1473 introduce any restrictions.
1474 \item[-] \texttt{using}\index{\texttt{using}} simlilar to
1475 \kbd{including}, allows total destruction.
1476 \end{itemize}
1477
1478 \begin{warning}
1479 To prove model-theoretic properties is very hard, and the
1480 system does not check whether indeed these restrictions are
1481 observed. It is on the user to choose correct modes.
1482 \end{warning}
1483
1484 As a special case, a couple of built-in modules are implicitly imported
1485 with \kbd{protecting} mode. In particular, \kbd{BOOL}\index{\texttt{BOOL}}
1486 is of practical importance. It defines Boolean operators. It
1487 is imported to admit conditional axioms. You may prohibit
1488 implicit importation by \kbd{set} command, as\index{\texttt{set}}
1489 \index{\texttt{include} (\texttt{BOOL})}
1490 \begin{vvtm}
1491 \begin{ccode}
1492 CafeOBJ> set include BOOL off
1493 \end{ccode}
1494 \end{vvtm}
1495 but be careful:
1496 Boolean operators are indispensable to write conditional axioms
1497 (Section \ref{sec:p2-eq-decl-cond}), and an attempt to use them
1498 will cause errors if \kbd{BOOL} is not imported.
1499
1500 Import declarations define a module hierarchy. We shall probe it
1501 later (Chapter \ref{sec:p2-module-hierarchies}).
1502
1503 \chapter{Signature}\label{sec:p2-signature}
1504
1505 A signature\index{signature}
1506 decides what is and is not a well-formed term (see
1507 Section \ref{sec:p2-wff-terms} for detail). A
1508 very basic definition would tell you that it consists of sort
1509 (name)s and operator (symbol)s paired with ranks.
1510 A signature for \cafeobj~is more elaborate. As briefly stated
1511 in the introductory chapter,
1512 \begin{itemize}
1513 \item The set of sorts may be partially ordered under inclusion, and
1514 \item There are two distinct kinds of sorts, with different concepts of
1515 equivalences.
1516 \end{itemize}
1517 These complications are reflected in the syntactic elements
1518 in several ways.
1519
1520 \section{Sort Declaration}\label{sec:p2-sort-declaration}
1521
1522 A sort\index{sort} is a set of classified elements, such as
1523 a set of designers, of supermodels, and of politicians.
1524 \cafeobj~has two kinds of sorts, hereafter called {\em visible sort\/}s
1525 and {\em hidden sort\/}s.\index{visible sort}\index{hidden sort}
1526 We leave it to you to decide whether the sort of politicians
1527 is visible or hidden.
1528
1529 \subsection{Sort Declaration}\label{sec:p2-sort-decl-syntax}
1530
1531 The simplest sort declaration is as follows.
1532
1533 \begin{bsyntax} visible sort declaration
1534 \index{\texttt{[},\texttt{]} (visible sort declarations)}
1535 ``\texttt{[}'' \textit{list\_of\_sort\_names} ``\texttt{]}''
1536 \end{bsyntax}
1537
1538 A sort name is an arbitrary character sequence, and a list of sort names
1539 is a list of such names separated by blanks. For example,
1540 \begin{vvtm}
1541 \begin{ccode}
1542 [ Nat ]
1543 \end{ccode}
1544 \end{vvtm}
1545 declares a single visible sort \texttt{Nat}, while
1546 \begin{vvtm}
1547 \begin{ccode}
1548 [ Nat Int Rat ]
1549 \end{ccode}
1550 \end{vvtm}
1551 declares visible sorts \texttt{Nat}, \texttt{Int}, and \texttt{Rat}.
1552 By replacing ``\verb|[|'' and ``\verb|]|'' with ``\verb|*[|'' and
1553 ``\verb|]*|'' respectively, you get a hidden sort declaration.
1554
1555 \begin{bsyntax} hidden sort declaration \Hline
1556 \index{\texttt{*[},\texttt{]*} (hidden sort declarations)}
1557 ``\texttt{*[}'' \textit{list\_of\_sort\_names} ``\texttt{]*}''
1558 \end{bsyntax}
1559
1560 In declaring operators (Section \ref{sec:p2-op-decl}) and
1561 axioms (Chapter \ref{sec:p2-axioms}), you have to
1562 distinguish visible sorts and hidden sorts. They
1563 give rise to different kinds of models, and are totally
1564 disjoint. Syntactically, you must not use the same name for
1565 both a visible and a hidden sort.
1566
1567 \begin{warning}
1568 The current implementation simply ignores dual declarations.
1569 For example, in the presence of a visible sort \kbd{S}, the
1570 declaration
1571 \begin{ccode}
1572 [ S ]
1573 \end{ccode}
1574 is silently ignored. But declaring diffrent type produces
1575 a warning message, for example
1576 \begin{snugshade}
1577 \begin{ccode}
1578 CafeOBJ> module M { *[ S ]* [ S ] }
1579 -- defining module M
1580 [Warning]: declaring a visible sort "S", there already be a sort
1581 with the same name but of differenct type (visible/hidden).
1582 ...ignored.._* done.
1583 \end{ccode}
1584 \end{snugshade}
1585 \end{warning}
1586
1587 \subsection{Subsort Declaration}
1588
1589 You may define a partial order over sets of sorts, which is
1590 interpreted as set inclusion.\index{partial order (over sorts)}
1591 Such an order is denoted with ``\verb|<|''. If \verb|A < B|,
1592 we say \verb|A| is a {\em subsort}\index{subsort} of \verb|B|,
1593 and \verb|B| a {\em supersort}\index{supersort}
1594 of \verb|A|. To declare this relation, you use
1595
1596 \begin{bsyntax} subsort declaration \index{\texttt{<}} \Hline
1597 ``\texttt{[}'' \textit{list\_of\_sort\_names} ``\texttt{<}'' \textit{list\_of\_sort\_names} $\{$ ``\texttt{<}'' \textit{list\_of\_sort\_names} $\}$ $*$ ``\texttt{]}''
1598 ``\texttt{*[}'' \textit{list\_of\_sort\_names} ``\texttt{<}'' \textit{list\_of\_sort\_names} $\{$ ``\texttt{<}'' \textit{list\_of\_sort\_names} $\}$ $*$ ``\texttt{]*}''
1599 \end{bsyntax}
1600
1601 ``\verb|[|'', ``\verb|]|'' enclose visible sorts and ``\verb|*[|'',
1602 ``\verb|]*|'' hidden ones, as before.
1603 A list of sort names is as in a sort declaration, but it can be
1604 qualified (qualification is explained in Section
1605 \ref{sec:p2-naming-and-qualification}). The above syntax means
1606 that each sort in the first list is a subsort of each sort in the
1607 second, each sort in the second is a subsort of each sort in
1608 the third, and so on. Note that ``\verb|<|'', although looking a
1609 strict relation, is non-strict, and means ``less than or equal to'', or
1610 ``subset of or equal to''.
1611
1612 For example, the usual inclusion of natural numbers \verb|Nat| into
1613 integers \verb|Int|, and \verb|Int| into rationals \verb|Rat|, may be
1614 written down as
1615 \begin{vvtm}
1616 \begin{ccode}
1617 [ Nat < Int < Rat ]
1618 \end{ccode}
1619 \end{vvtm}
1620 or
1621 \begin{vvtm}
1622 \begin{ccode}
1623 [ Int < Rat ]
1624 [ Nat < Int ]
1625 \end{ccode}
1626 \end{vvtm}
1627 or
1628 \begin{vvtm}
1629 \begin{ccode}
1630 [ Nat Int < Rat ]
1631 [ Nat < Int ]
1632 \end{ccode}
1633 \end{vvtm}
1634 For a briefer description, you may put more than one declarations within
1635 a single bracket pair, using commas for separation. For example, the last
1636 declarations above may be abbreviated to
1637 \begin{vvtm}
1638 \begin{ccode}
1639 [ Nat Int < Rat, Nat < Int ]
1640 \end{ccode}
1641 \end{vvtm}
1642 It is also possible to combine sort and subsort declarations into a
1643 single bracket pair, as
1644 \begin{vvtm}
1645 \begin{ccode}
1646 [ Nat Int Rat, Nat < Int < Rat ]
1647 \end{ccode}
1648 \end{vvtm}
1649 which is equivalent to
1650 \begin{vvtm}
1651 \begin{ccode}
1652 [ Nat Int Rat ]
1653 [ Nat < Int < Rat ]
1654 \end{ccode}
1655 \end{vvtm}
1656 \begin{warning}
1657 In subsort declarations, if new sort names appear, they are
1658 assumed to be declared implicitly. If \texttt{Nat}, \texttt{Int} and
1659 \texttt{Rat} have not been declared, therefore,
1660 \begin{vvtm}
1661 \begin{ccode}
1662 [ Nat < Int < Rat ]
1663 \end{ccode}
1664 \end{vvtm}
1665 and
1666 \begin{vvtm}
1667 \begin{ccode}
1668 [ Nat Int Rat, Nat < Int < Rat ]
1669 \end{ccode}
1670 \end{vvtm}
1671 are interpreted identically. Beware, especially, of mistyping
1672 upper/lower cases. For example, if you imported a sort
1673 \verb|Nat| and declared
1674 \begin{vvtm}
1675 \begin{ccode}
1676 [ NAT < Int ]
1677 \end{ccode}
1678 \end{vvtm}
1679 \verb|NAT| is regarded as a newly declared sort, not an imported
1680 one.
1681 \end{warning}
1682
1683 It is abnormal for subsort relation to be circular, and if they were
1684 circular, you should not expect the system to behave correctly.
1685 Subsort relation is set inclusion, so circular relation
1686 means that all the sorts involved are the same. But \cafeobj~does not
1687 recomment such indirect ``renaming'', and may refuse to
1688 support this interpretation.
1689
1690 % \begin{warning}
1691 % In general, checks on circularity are hard to implement efficiently, so
1692 % the system only checks obvious cases. If circularity is detected, you
1693 % are severely warned.
1694 % \end{warning}
1695
1696 Note that it is impossible to make a visible sort a subsort of
1697 a hidden sort, or vice versa. You cannot put a hidden sort in
1698 plain brackets, nor a visible sort in asterisked brackets.
1699 This impossibility reflects the fact
1700 that visible sorts are disjoint from hidden sorts.
1701
1702 \section{Operator Declaration}\label{sec:p2-operator}
1703
1704 In traditional algebraic specification languages, an operator\index{operator}
1705 is a function, i.e. an unambiguous, static mapping.
1706 \cafeobj~extends the concept in two directions.
1707 An operator may {\em transform} an element into another.
1708 Such an operator is defined by transition relations
1709 (Section \ref{sec:p2-trans-decl}). Moreover, an operator
1710 may change an {\em internal state} of an element. Such an
1711 operator is defined from an observational viewpoint.
1712
1713 \subsection{Operator Declaration}\label{sec:p2-op-decl}
1714
1715 Like the other strongly-typed languages, \cafeobj~requires you to make
1716 explicit the domains and
1717 codomains of operators, and only allows terms constructed accordingly.
1718 Since \cafeobj~is order-sorted, however, well-formedness involves
1719 subtle considerations (see Section \ref{sec:p2-wff-terms}).
1720
1721 In addition to domains and codomains, operator declarations determine
1722 term constructs.
1723 \cafeobj~allows you to employ various notations in writing terms,
1724 such as infix (e.g. $2 + 4$), prefix ($- 4$), and postfix ($2!$).
1725 Hence in declaring operators, you have a way to adopt your favourite
1726 notational practice. If you do not show any preference, you get
1727 a standard notation for function application.
1728 For example, given a (ternary) operator \verb|f|,
1729 a term thereof is constructed out of parenthesised, comma separated
1730 arguments, such as \verb|f(a,b,c)|.
1731 A declaration for such an operator is called {\em standard}.
1732
1733 \begin{bsyntax} standard operator declaration \index{\texttt{op}} \Hline
1734 \index{standard operator declaration}
1735 \texttt{op} \textit{standard\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names} ``\texttt{->}'' \textit{sort\_name}
1736 \end{bsyntax}
1737
1738 A standard operator symbol is an arbitrary character string that
1739 does not contain an underbar ``\verb|_|''. A list of
1740 sort names is a blank-separated list and designates the arity\index{arity}
1741 of the operator. The sort name after ``\verb|->|'' designates its
1742 coarity\index{coarity}.
1743 Sort names in arities and coarities may be qualified
1744 (cf. Section \ref{sec:p2-naming-and-qualification}).
1745 The pair of the arity and the coarity is called the rank\index{rank}
1746 of the operator. An operator with the empty arity is a
1747 constant\index{constant}.
1748
1749 \begin{warning}
1750 It is possible for a standard operator symbol to contain blanks. For
1751 example, in
1752 \begin{vvtm}
1753 \begin{ccode}
1754 op a constant of sort S : -> S
1755 \end{ccode}
1756 \end{vvtm}
1757 the whole ``\verb|a constant of sort S|'' is a symbol. More precisely,
1758 the system treats the symbol as a blank-separated list of non-blank
1759 character strings, and arbitrary numbers of blanks may be inserted.
1760
1761 But parsing terms that contain such symbols tends to be erroneous, and
1762 you often have to enclose the symbol with parentheses. For this reason,
1763 it is advisable not to use blanks, unless your aesthetics so forces.
1764 \end{warning}
1765
1766 If not standard, a declaration is called {\em mixfix}.
1767
1768 \begin{bsyntax} mixfix operator declaration \Hline
1769 \index{mixfix operator declaration}
1770 \texttt{op} \textit{mixfix\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names} ``\texttt{->}'' \textit{sort\_name}
1771 \end{bsyntax}
1772
1773 A mixfix operator symbol may contain underbars. Arities and coarities
1774 are the same as in standard operator declarations. In fact,
1775 the only difference from standard declarations is the presence of
1776 underbars. Underbars reserve the places where arguments are inserted.
1777 The leftmost underbar is for the first argument, the next leftmost the
1778 second, and so on. The number of underbars must match the number
1779 of sort names in the arity.
1780
1781 \begin{warning}
1782 The single underbar cannot be an operator symbol; it would introduce
1783 invisible operator applications. Such a declaration is
1784 ignored, and you are ridiculed by the system.
1785 \end{warning}
1786
1787 For example, addition is usually written in the infix notation. Declare
1788 \begin{vvtm}
1789 \begin{ccode}
1790 op _+_ : Nat Nat -> Nat
1791 \end{ccode}
1792 \end{vvtm}
1793 and terms such as ``\texttt{N + M}'', ``\texttt{s(0) + N}'' are admissible.
1794 Another representative example is a conditional expression.
1795 \begin{vvtm}
1796 \begin{ccode}
1797 op if_then_else_fi : Bool Nat Nat -> Nat
1798 \end{ccode}
1799 \end{vvtm}
1800 According to the definition, this declaration allows you to write
1801 \begin{vvtm}
1802 \begin{ccode}
1803 if N < M then N else M fi
1804 \end{ccode}
1805 \end{vvtm}
1806 and
1807 \begin{vvtm}
1808 \begin{ccode}
1809 if true then 0 else s(0) fi
1810 \end{ccode}
1811 \end{vvtm}
1812 Since \cafeobj~allows operator overloading, it is
1813 possible to use the same operator symbol for different operators.
1814 For example, the symbol \verb|_+_| may be overloaded as follows.
1815 \begin{vvtm}
1816 \begin{ccode}
1817 op _+_ : Nat Nat -> Nat
1818 op _+_ : Set Set -> Set
1819 \end{ccode}
1820 \end{vvtm}
1821 An operator symbol, whether standard or mixfix, does not only define
1822 term constructs, but acts as an operator name. What an operator name
1823 refers to is a nontrivial question, due to overloading possibilities
1824 (see Section \ref{sec:p2-operator-reference} for detail).
1825
1826 You can declare several operators simultaneously, if they have exactly
1827 the same rank:
1828 use \verb|ops| instead of \verb|op|, as\index{\texttt{ops}}
1829 \begin{vvtm}
1830 \begin{ccode}
1831 ops (_+_) (_*_) : Nat Nat -> Nat
1832 \end{ccode}
1833 \end{vvtm}
1834 which means the same as
1835 \begin{vvtm}
1836 \begin{ccode}
1837 op _+_ : Nat Nat -> Nat
1838 op _*_ : Nat Nat -> Nat
1839 \end{ccode}
1840 \end{vvtm}
1841 Note that, in simultaneous declarations, parentheses are sometimes necessary
1842 to separate operator symbols.
1843
1844 \begin{warning}
1845 A general advice: if you are in doubt, enclose with parentheses.
1846 Parentheses help you avoid many parsing ambiguities.
1847 \end{warning}
1848
1849 \subsection{Behavioural Operator Declaration}\label{sec:p2-beh-op-decl}
1850
1851 For hidden sorts, you need a special kind of operators, called
1852 {\em behavioural operator\/}s.\index{behavioural operator}
1853
1854 \begin{bsyntax} behavioural operator declaration \index{\texttt{bop}} \Hline
1855 \texttt{bop} \textit{standard\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names} ``\texttt{->}'' \textit{sort\_name}
1856 \texttt{bop} \textit{mixfix\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names} ``\texttt{->}'' \textit{sort\_name}
1857 \end{bsyntax}
1858
1859 Standard operator symbols, mixfix operator symbols, lists of sort names
1860 and sort names are as in \verb|op| in the previous section.
1861 It is required that
1862 \begin{itemize}
1863 \item The arity of a behavioural operator contain exactly one
1864 hidden sort.
1865 \end{itemize}
1866 Intuitively, you may regard a hidden sort as consisting of objects, which
1867 have internal states. Then a behavioural operator
1868 is an {\em attribute}\index{attribute} or a {\em method}\index{method},
1869 if you really, really want to adopt the parlance of a certain community.
1870 As an illustration,
1871 consider bank account objects, whose balances change upon deposits and
1872 withdrawals.
1873 \begin{vvtm}
1874 \begin{ccode}
1875 *[ Account ]*
1876 bop deposit : Account Nat -> Account
1877 bop withdraw : Account Nat -> Account
1878 bop balance : Account -> Nat
1879 \end{ccode}
1880 \end{vvtm}
1881 Here \verb|deposit|, \verb|withdraw| are operations that modify
1882 internal states --- balances --- of accounts (so they are methods),
1883 while \verb|balance| reads the current balances of accounts
1884 (so an attribute).
1885
1886 The above intuition tells you why a behavioural operator should have
1887 exactly one hidden sort in its arity. It does not make sense to
1888 retrieve attributes from two objects. And it is nonsense to talk about
1889 attributes of nothing. Technically, behavioural operators, and only them,
1890 make behavioural contexts (see Section \ref{sec:p2-hsort-beget-axs}).
1891
1892 There is a counterpart to \verb|ops|, with which you may
1893 declare several behavioural operators at once.
1894 Two of the above declarations may be
1895 replaced by\index{\texttt{bops}}
1896 \begin{vvtm}
1897 \begin{ccode}
1898 bops deposit withdraw : Account Nat -> Account
1899 \end{ccode}
1900 \end{vvtm}
1901 Note that an operator symbol may be overloaded among behavioural operators,
1902 and/or among the mixture of usual and behavioural operators. Note
1903 further that a non-behavioural operator declaration with
1904 \verb|op| is {\em not} required to contain or not to contain
1905 hidden sorts. You may declare non-behavioural operators on hidden sorts, as
1906 \begin{vvtm}
1907 \begin{ccode}
1908 op deposit : Account Nat -> Account
1909 \end{ccode}
1910 \end{vvtm}
1911 The effects are, however, very different. Do take care.
1912
1913 \subsection{Predicate Declaration}\label{sec:p2-pred-decl}
1914
1915 A {\em predicate}\index{predicate}, in \cafeobj, is nothing more
1916 than an operator which has a special sort \verb|Bool| as its coarity.
1917 This sort is contained in the module \verb|BOOL|, which is imported
1918 everywhere (Section \ref{sec:p2-import-decl}).
1919 \verb|Bool| is important in several respects, and is accorded a privilege
1920 by the following shorthand constructs.
1921
1922 \begin{bsyntax} predicate declaration \index{\texttt{pred}} \Hline
1923 \texttt{pred} \textit{standard\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names}
1924 \texttt{pred} \textit{mixfix\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names}
1925 \end{bsyntax}
1926
1927 Operator symbols etc. are as in operator declarations. Predicate
1928 declarations are equivalent to operator declarations with
1929 coarity \verb|Bool|. For example,
1930 \begin{vvtm}
1931 \begin{ccode}
1932 pred _<_ : Nat Nat
1933 \end{ccode}
1934 \end{vvtm}
1935 has the same meaning as
1936 \begin{vvtm}
1937 \begin{ccode}
1938 op _<_ : Nat Nat -> Bool
1939 \end{ccode}
1940 \end{vvtm}
1941 \begin{warning}
1942 The system does not distinguish predicates as such.
1943 You should think of them as mere shorthands. Nothing more.
1944 \end{warning}
1945
1946 To complete the picture,
1947 \verb|bpred| is a shorthand of \verb|bop| with coarity \verb|Bool|.
1948
1949 \begin{bsyntax} behavioural predicate declaration \index{\texttt{bpred}} \Hline
1950 \texttt{bpred} \textit{standard\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names}
1951 \texttt{bpred} \textit{mixfix\_operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names}
1952 \end{bsyntax}
1953
1954 \section{Terms}\label{sec:p2-terms}
1955
1956 \subsection{Well-formed Terms}\label{sec:p2-wff-terms}
1957
1958 As well as adopting arbitrary notational practices, you may
1959 use the same notation to denote different operators, such as
1960 $1 + 2$ for addition and $\{ a \} + \{ a, b \}$ for
1961 direct sum. These flexibilities greatly enhance the
1962 legibility of \cafeobj~codes, and we believe it worthwhile to support
1963 them.
1964
1965 One disadvantage of this liberty is the extra amount of processing time
1966 in parsing terms. Another, and more important, problem is that a term may
1967 be parsed in more than one ways. We make recourse to the following
1968 definition.
1969 \begin{quote}
1970 A term is {\em well-formed}\index{well-formed term} iff it has
1971 exactly one least parse tree.
1972 \end{quote}
1973 Several parse trees are okay, so long as all of them are of
1974 related sorts, and one of them has a {\em least sort}.\index{least sort}
1975 The least sort is the smallest set to which the element belongs.
1976 Dual membership is a seed of troubles, just as dual marriage is.
1977
1978 A signature is {\em regular}\index{regular signature} iff all the
1979 terms have least sorts, and a module is regular iff its signature is.
1980 With the above observations in mind, you understand
1981 this is an important property.
1982 \cafeobj~has a mechanism to regularise signatures. To invoke this
1983 mechanism, you may use
1984
1985 \begin{bsyntax} \texttt{regularize} command \index{\texttt{regularize}} \Hline
1986 \texttt{regularize} \textit{module\_name}
1987 \end{bsyntax}
1988
1989 or set a switch for automatic invocation to \verb|on| by\index{\texttt{set}}
1990 \index{\texttt{signature}}
1991 \begin{vvtm}
1992 \begin{ccode}
1993 CafeOBJ> set regularize signature on
1994 \end{ccode}
1995 \end{vvtm}
1996 \index{\texttt{signature}}
1997 The default is \verb|off|. To regularise a signature, additional sorts and
1998 operator declarations may be declared. These addidions ensure the existence
1999 of ``lowest'' operators. For a trivial example, in
2000 \begin{vvtm}
2001 \begin{ccode}
2002 module INTB {
2003 [ NonPos Nat < Int ]
2004 op 0 : -> NonPos
2005 op 0 : -> Nat
2006 }
2007 \end{ccode}
2008 \end{vvtm}
2009 \label{exs:intb}
2010 the constant \verb|0| belongs both to \verb|NonPos| and to \verb|Nat|.
2011 In general it is possible to overload constants, there is a way to
2012 distinguish them (see Section \ref{sec:p2-parsing-qualifier}), and
2013 there is no harm in that. But in this example, since \verb|NonPos|
2014 and \verb|Nat| are both subsorts of \verb|Int|, the constant must
2015 in fact denote the same element. As a consequence, the term \verb|0|
2016 does not have a least parse (even qualified,
2017 as in Section \ref{sec:p2-parsing-qualifier}).
2018 A remedy is to add a common subsort of \verb|NonPos| and \verb|Nat|,
2019 and declare \verb|0| of that sort.
2020 Using \verb|regularize| command, you do get a module modified along
2021 this line:
2022 \begin{vvtm}
2023 \begin{ccode}
2024 module INTB {
2025 [ NonPos^Nat < NonPos Nat < Int ]
2026 op 0 : -> NonPos
2027 op 0 : -> Nat
2028 op 0 : -> NonPos^Nat
2029 }
2030 \end{ccode}
2031 \end{vvtm}
2032 Two old declarations of \verb|0| are redundant by now, but system
2033 does not remove these \verb|0|s in automatic.
2034
2035 % \begin{warning}
2036 % The current version does not remove redundant \verb|0|'s.
2037 % \end{warning}
2038
2039 More generally, the system searches for operators that may create
2040 terms with no least parse, and declare such operators on least sorts
2041 after, if necessary, adding new common subsorts. What the system
2042 checks is a sufficient, but {\em not} necessary, condition. There are
2043 cases the system is overprotective. As an example, consider
2044 \begin{vvtm}
2045 \begin{ccode}
2046 module INTB' {
2047 [ NonPos Nat < Int ]
2048 op _+_ : NonPos NonPos -> NonPos
2049 op _+_ : Nat Nat -> Nat
2050 op _+_ : Int Int -> Int
2051 op 1 : -> Nat
2052 op -1 : -> NonPos
2053 }
2054 \end{ccode}
2055 \end{vvtm}
2056 \label{exs:intb'}
2057 \verb|INTB'| is regular: a term is of the form of a binary tree where
2058 all the nodes contain \verb|+| and all the leaves contain \verb|1| or
2059 \verb|-1|, and is of sort \verb|NonPos| iff every leaf is \verb|-1|,
2060 of sort \verb|Nat| iff every leaf is \verb|1|, and of sort \verb|Int|
2061 iff otherwise. If you invoke \verb|regularize| command, however, you get
2062 a modified module:
2063 \begin{vvtm}
2064 \begin{ccode}
2065 module INTB' {
2066 [ NonPos^Nat < NonPos Nat < Int ]
2067 op _+_ : NonPos^Nat NonPos^Nat -> NonPos^Nat
2068 op _+_ : NonPos NonPos -> NonPos
2069 op _+_ : Nat Nat -> Nat
2070 op _+_ : Int Int -> Int
2071 op 1 : -> Nat
2072 op -1 : -> NonPos
2073 }
2074 \end{ccode}
2075 \end{vvtm}
2076 The reason is as follows. If \verb|Nat|, \verb|NonPos| are both
2077 inhabited, there is a danger that their intersection is not
2078 empty. If it is indeed non-empty,
2079 an application of ``\verb|_+_|'' on any terms in it
2080 creates a term both of sort
2081 \verb|Nat| and of \verb|NonPos|. So it is safe to add a new common
2082 subsort, and declare ``\verb|_+_|'' on it.
2083
2084 To check the non-emptiness is too hard to solve mechanically.
2085 For example, if you insert an equation (Section \ref{sec:p2-equation-decl})
2086 \begin{vvtm}
2087 \begin{ccode}
2088 eq 1 = -1 .
2089 \end{ccode}
2090 \end{vvtm}
2091 into \verb|INTB'|, there does exist an element, namely \verb|1|
2092 (or \verb|-1|), which belongs to both \verb|NonPos| and \verb|Nat|.
2093 Thus a general solution requires a complete equational calculus,
2094 which is beyond the power of \cafeobj. It is for this reason that
2095 the system always bets on the safer side, even if the results are dull:
2096 the system (and you) will not lose a penny, since the modified
2097 module has exactly the same model(s) as before (when you take reducts).
2098
2099 If one of \verb|NonPos| or \verb|Nat| were empty, the system would have done
2100 nothing. For example, if you delete the declaration
2101 \begin{vvtm}
2102 \begin{ccode}
2103 op 1 : -> Nat
2104 \end{ccode}
2105 \end{vvtm}
2106 from \verb|INTB'|, \verb|Nat| is not inhabited any more: the system
2107 adds nothing this time.
2108
2109 For those who only want to be warned
2110 of the danger, it is also possible just to check if the signature is regular
2111 (cf. Section \ref{sec:p2-check-command}).
2112
2113 \subsection{Parsing Terms}\label{sec:p2-parsing-qualifier}
2114
2115 As suggested above, a symbol sequence may be parsed in more than one ways.
2116 A salient example is a constant symbol. With the two declarations below,
2117 \verb|empty| is a term both of \verb|Stack| and \verb|Set|.
2118 \begin{vvtm}
2119 \begin{ccode}
2120 op empty : -> Stack
2121 op empty : -> Set
2122 \end{ccode}
2123 \end{vvtm}
2124 In such cases, you have to supply additional information by qualification.
2125 Other ambiguities may be eliminated by parentheses. Let us see a couple of
2126 examples.
2127
2128 To see whether a term is unambiguous, the following command is handy.
2129
2130 \begin{bsyntax} parsing command \index{\texttt{parse}} \Hline
2131 \index{\texttt{.} (term stopper)}
2132 \texttt{parse} $[$\texttt{in} \textit{module\_expression} ``\texttt{:}''$]$ \textit{term} ``\texttt{.}''
2133 \end{bsyntax}
2134
2135 If \verb|in| option is omitted, the current module\index{current module}
2136 (cf. Section \ref{sec:p2-current-module}) is assumed. Module expressions
2137 are explained in Section \ref{sec:p2-module-expression}. It is enough
2138 here to know that a module name is a module expression.
2139
2140 \begin{warning}
2141 Do not forget the last period, preceded by a blank. This is the only
2142 way for the system to detect the end of a term: usual parsing techniques
2143 would not do, due to flexible term constructs. The same convention is
2144 used in other commands and declarations.
2145 \end{warning}
2146
2147 To see how this command works, here is an example where an ambigouous term
2148 is parsed.
2149 \label{exs:simple-int}
2150 \begin{vvtm}
2151 \begin{ccode}
2152 module SIMPLE-INT {
2153 protecting (SIMPLE-NAT)
2154 [ Nat NegInt < Int ]
2155 op -_ : NzNat -> NegInt
2156 op _+_ : Int Int -> Int
2157 op _-_ : Int Int -> Int
2158 op s : Int -> Int
2159 vars M M' : Int
2160 }
2161 \end{ccode}
2162 \end{vvtm}
2163 The above module gives a signature for integer operations
2164 (\verb|SIMPLE-NAT| was in Section \ref{exs:simple-nat}).
2165 \verb|vars| declares
2166 variables (Section \ref{sec:p2-variable-decl}). After declaring
2167 the above module, you may get a session like
2168 %\newpage
2169 \begin{vvtm}
2170 \begin{ccode}
2171 CafeOBJ> parse in SIMPLE-INT : M - M' - s(0) .
2172 [Warning]: Ambiguous term:
2173 please try `check regularity' command.
2174 if the signature is regular, there possibly be
2175 some name conflicts between operators and variables.
2176 [1] _-_ : Int Int -> Int ------------------------((M:Int - M':Int) - s(0))
2177 [2] _-_ : Int Int -> Int ------------------------(M:Int - (M':Int - s(0)))
2178 [Error] no successful parse
2179 ("ambiguous term"):SyntaxErr
2180 \end{ccode}
2181 \end{vvtm}
2182 If you \kbd{set} a switch \kbd{verbose} to \kbd{on},
2183 you see a more visible tree structure of possible parses like
2184 \begin{vvtm}
2185 \begin{ccode}
2186 CafeOBJ> set verbose on
2187 CafeOBJ> parse in SIMPLE-INT : M - M' - s(0) .
2188
2189 [Warning]: Ambiguous term:* Please select a term from the followings:
2190 [1] _-_ : Int Int -> Int --------------------------------------------
2191 _-_:Int
2192 / \
2193 _-_:Int s:NzNat
2194 / \ |
2195 M:Int M':Int 0:Zero
2196
2197 [2] _-_ : Int Int -> Int --------------------------------------------
2198 _-_:Int
2199 / \
2200 M:Int _-_:Int
2201 / \
2202 M':Int s:NzNat
2203 |
2204 0:Zero
2205
2206 [Error] no successful parse
2207 ("ambiguous term"):SyntaxErr
2208
2209 CafeOBJ>
2210 \end{ccode}
2211 \end{vvtm}
2212 If the term is ambiguous, as in this example, the system gives a
2213 warning, as showed in the above.
2214 And if you \kbd{set} switch \kbd{select term} to \kbd{on},
2215 you have an option to select a parse from possible choces.
2216 \begin{vvtm}
2217 \begin{ccode}
2218 CafeOBJ> set verbose off
2219 CafeOBJ> set select term on
2220 CafeOBJ> parse in SIMPLE-INT : M - M' - s(0) .
2221 [Warning]: Ambiguous term:
2222 please try `check regularity' command.
2223 if the signature is regular, there possibly be
2224 some name conflicts between operators and variables.
2225 * Please select a term from the followings:
2226 [1] _-_ : Int Int -> Int ------------------------((M:Int - M':Int) - s(0))
2227 [2] _-_ : Int Int -> Int ------------------------(M:Int - (M':Int - s(0)))
2228 * Please input your choice (a number from 1 to 2, default is 1)? 2
2229 Taking the second as correct.
2230 (M - (M' - s(0))):Int
2231 CafeOBJ>
2232 \end{ccode}
2233 \end{vvtm}
2234 The number after the message ``\verb|*| \verb|Please ...|'' is
2235 your input.
2236
2237 \begin{warning}
2238 The system is impatient. If you hesitate long, it automatically
2239 selects the default (in the above case, \verb|1|).
2240 \end{warning}
2241
2242 The result of the parsing was
2243 \begin{vvtm}
2244 \begin{ccode}
2245 (M - (M' - s(0))) : Int
2246 \end{ccode}
2247 \end{vvtm}
2248 where the term is parenthesised to reveal connectivity.
2249 \verb|Int| on the right of ``\verb|:|'' is its least sort.
2250
2251 \subsection{Qualifying Terms}\label{sec:p2-qualifying-terms}
2252
2253 Ambiguity that arises from operator connectivity, e.g.
2254 \begin{vvtm}
2255 \begin{ccode}
2256 M - M' - s(0)
2257 \end{ccode}
2258 \end{vvtm}
2259 above, can be eliminated by parentheses. On the other hand, ambiguity caused
2260 by overloading cannot be so easily eliminated. It requires additional
2261 sort information. Hence term qualification\index{term qualification}, as
2262 \index{qualified term}
2263
2264 \begin{bsyntax} sort-qualified term \Hline
2265 ``\texttt{(}'' \textit{term} ``\texttt{)}'' ``\texttt{:}'' \textit{sort\_name}
2266 \end{bsyntax}
2267
2268 \begin{warning}
2269 This version, unlike former versions, allows blanks around ``\texttt{:}''.
2270 The following terms are all valid and parsed as the same.
2271 \begin{vvtm}
2272 \begin{ccode}
2273 (empty):Stack
2274 (empty): Stack
2275 (empty) :Stack
2276 (empty) : Stack
2277 \end{ccode}
2278 \end{vvtm}
2279 \end{warning}
2280
2281 \begin{warning}
2282 Be sure to put parentheses around the qualified. \cafeobj~allows
2283 inline variable declarations, such as
2284 \begin{vvtm}
2285 \begin{ccode}
2286 M:Nat - s(0)
2287 \end{ccode}
2288 \end{vvtm}
2289 where \verb|M| is regarded (at this place only) as a variable of
2290 sort \verb|Nat|. Without parentheses, e.g.
2291 \begin{vvtm}
2292 \begin{ccode}
2293 empty:Stack
2294 \end{ccode}
2295 \end{vvtm}
2296 is regarded as a new variable!
2297 \end{warning}
2298
2299 For example, consider the following module declarations.
2300 \begin{vvtm}
2301 \begin{ccode}
2302 module OCTAL {
2303 [ OctalDigit ]
2304 ops 0 1 2 3 4 5 6 7 : -> OctalDigit
2305 }
2306
2307 module HEX {
2308 [ HexDigit ]
2309 ops 0 1 2 3 4 5 6 7 8 9 : -> HexDigit
2310 ops A B C D E F : -> HexDigit
2311 }
2312
2313 module HEX+OCTAL {
2314 protecting (OCTAL)
2315 protecting (HEX)
2316 }
2317 \end{ccode}
2318 \end{vvtm}
2319 Since \verb|HEX+OCTAL| contains declarations either in \verb|OCTAL| or in
2320 \verb|HEX|, the digits \verb|0| through \verb|7| are ambiguous there:
2321 they are constants both of \verb|OctalDigit| and \verb|HexDigit|.
2322 Indeed, try parsing \verb|0| and you get a warning.
2323 \begin{vvtm}
2324 \begin{ccode}
2325 CafeOBJ> parse in HEX+OCTAL : 0 .
2326 [Warning]: Ambiguous term:
2327 please try `check regularity' command.
2328 if the signature is regular, there possibly be
2329 some name conflicts between operators and variables.
2330 [1] 0 : -> HexDigit --------------------------------0
2331 [2] 0 : -> OctalDigit ----------------------------0
2332 [Error] no successful parse
2333 ("ambiguous term"):SyntaxErr
2334 CafeOBJ>
2335 \end{ccode}
2336 \end{vvtm}
2337 To eliminate this ambiguity, you may qualify by sorts. Try again with
2338 \begin{vvtm}
2339 \begin{ccode}
2340 CafeOBJ> parse in HEX+OCTAL : (0):OctalDigit .
2341 \end{ccode}
2342 \end{vvtm}
2343 and the system will oblige with
2344 \begin{vvtm}
2345 \begin{ccode}
2346 0 : OctalDigit
2347 \end{ccode}
2348 \end{vvtm}
2349
2350 %% \section{Record Declaration}\label{sec:p2-record-declaration}
2351
2352 %% A {\em record} declaration\index{record declaration} defines a record-type
2353 %% data structure. A record type consists of fields, and an element of the
2354 %% type is completely determined by the value of each field.
2355 %% In \cafeobj, a field is called {\em slot}\index{slot}.
2356
2357 %% \subsection{Plain Record Declaration}\label{sec:p2-plain-record-decl}
2358
2359 %% A record declaration without inheritance (see the next section) is as
2360 %% follows.
2361
2362 %% \begin{bsyntax} plain record declaration \index{\texttt{record}} \Hline
2363 %% \texttt{record} \textit{record\_name} ``\texttt{\{}'' \textit{list\_of\_slot\_declarations} ``\texttt{\}}''
2364 %% \end{bsyntax}
2365
2366 %% where a list of slot declarations is a blank-separated list.
2367 %% A slot declaration has the following construct.
2368
2369 %% \begin{bsyntax} slot declaration \Hline
2370 %% \textit{slot\_name} ``\texttt{:}'' \textit{sort\_name}
2371 %% \end{bsyntax}
2372
2373 %% where a slot name is a character string and a sort name is
2374 %% the name of an already declared sort. For example, if \verb|Nat|
2375 %% is a sort of natural numbers with usual digit notations,
2376 %% \begin{vvtm}
2377 %% \begin{ccode}
2378 %% record Date {
2379 %% year : Nat
2380 %% month : Nat
2381 %% day : Nat
2382 %% }
2383 %% \end{ccode}
2384 %% \end{vvtm}
2385 %% is a sloppy definition of records that consist of years, months, and days.
2386 %% (Sloppy because it admits e.g. 554/32/1984 as a date.)
2387
2388 %% In ordinary programming languages, once a record is declared,
2389 %% notation/functions to access fields are automatically determined.
2390 %% The dotted notation \verb|d.day|, where \verb|d| is a record variable
2391 %% and \verb|day| is a field name, is typical.
2392 %% \cafeobj~also provides access functions. In fact, a record is nothing more
2393 %% than a special sort. The effects of a record declaration are as follows.
2394 %% \begin{itemize}
2395 %% \item A sort with the record name is declared. A record is a sort,
2396 %% so the name can appear wherever a sort name can.
2397 %% \item A mixfix operator is declared, so that a term of the form
2398 %% \begin{vvtm}
2399 %% \begin{ccode}
2400 %% record-name { slot-name1 = value1, slot-name2 = value2, ... }
2401 %% \end{ccode}
2402 %% \end{vvtm}
2403 %% is an element of \verb|record-name|.
2404 %% Using the above example,
2405 %% \begin{vvtm}
2406 %% \begin{ccode}
2407 %% Date { year = 1984, month = 1, day = 1 }
2408 %% \end{ccode}
2409 %% \end{vvtm}
2410 %% is a term of sort \verb|Date|. Moreover, the slot-value pairs
2411 %% in the braces may be written in whatever order, and you may even
2412 %% omit some of them. Thus
2413 %% \begin{vvtm}
2414 %% \begin{ccode}
2415 %% Date { month = 1, day = 1, year = 1984 }
2416 %% Date { year = 1984 }
2417 %% \end{ccode}
2418 %% \end{vvtm}
2419 %% are well-formed terms of sort \verb|Date|. Note that \verb|Date| in
2420 %% these terms is part of an operator symbol, not the sort name
2421 %% which happens to be the same character string.
2422 %% \item For each slot, two access functions are declared and defined.
2423 %% In case of \verb|Date|, it is as if the declarations
2424 %% \begin{vvtm}
2425 %% \begin{ccode}
2426 %% op year : Date -> Nat
2427 %% op set-year : Date Nat -> Date
2428 %% -- similarly for month and day
2429 %% \end{ccode}
2430 %% \end{vvtm}
2431 %% are given. Accordingly, to get the value of \verb|year| slot
2432 %% from a term \verb|d| of sort \verb|Date|, you can write \verb|year(d)|;
2433 %% and to set the slot to \verb|y|, write \verb|set-year(d,y)|.
2434 %% \end{itemize}
2435 %% Strictly speaking, since the definitions of access functions are by
2436 %% axioms (axioms are to be explained in Chapter \ref{sec:p2-axioms}), a
2437 %% record declaration involve axioms as well as signatures.
2438 %% You may investigate all the above effects by \verb|show| command
2439 %% (cf. Section \ref{sec:p2-record-inspection}).
2440
2441 %% \subsection{Inheriting Records}\label{sec:p2-record-inheritance}
2442
2443 %% You can extend a record to create a new record.
2444 %% The same access functions can be used as before.
2445 %% The process of extending a record is called
2446 %% {\em record inheritance}\index{record inheritance},
2447 %% {\` a} la peculiar parlance of some community, and
2448 %% is denoted by the following construct.
2449
2450 %% \begin{bsyntax} record declaration with inheritance \index{\texttt{record}} \Hline
2451 %% \texttt{record} \textit{record\_name} ``\texttt{[}'' \textit{list\_of\_upper\_records} ``\texttt{]}'' ``\texttt{\{}'' \textit{list\_of\_slot\_declarations} ``\texttt{\}}''
2452 %% \end{bsyntax}
2453
2454 %% A list of slot declarations is as in a plain record declaration.
2455 %% A list of upper records is comma-separated. An upper record is of
2456 %% the form
2457
2458 %% \begin{bsyntax} upper record \Hline
2459 %% \textit{record\_name} $[$``\texttt{*}'' ``\texttt{(}'' \textit{slot\_mapping} ``\texttt{)}''$]$
2460 %% \end{bsyntax}
2461
2462 %% where a slot mapping is
2463
2464 %% \begin{bsyntax} slot mapping \Hline
2465 %% \textit{slot\_name} ``\texttt{->}'' \textit{slot\_name} $\{$ ``\texttt{,}'' \textit{slot\_name} ``\texttt{->}'' \textit{slot\_name} $\}$ $*$
2466 %% \end{bsyntax}
2467
2468 %% where slot names before ``\verb|->|'' must be the ones belonging to the
2469 %% given record, while names after ``\verb|->|'' are arbitrary (so long as
2470 %% not conflicting).
2471
2472 %% Under a record declaration with inheritance,
2473 %% the newly declared record inherits all the slots from
2474 %% the upper records. Slot mappings, which specify renaming,
2475 %% are to be used to avoid name conflicts.
2476 %% \cafeobj~does not allow slots of the same name to be inherited
2477 %% twice. If there is no conflict, slot mappings may be omitted.
2478
2479 %% An %boring
2480 %% example of inheritance, using \verb|Date| of the previous
2481 %% section, is
2482 %% \begin{vvtm}
2483 %% \begin{ccode}
2484 %% record Time [ Date ] {
2485 %% hour : Nat
2486 %% minute : Nat
2487 %% }
2488 %% \end{ccode}
2489 %% \end{vvtm}
2490 %% This code declares \verb|Time| to have slots \verb|year|, \verb|month|,
2491 %% and \verb|day|, as well as \verb|hour| and \verb|minute|.
2492 %% Access functions \verb|year|, \verb|set-year|, and so on are
2493 %% applicable to terms of \verb|Time| exactly as they are
2494 %% to those of \verb|Date|.
2495 %% The effects of the declaration are equivalent to
2496 %% \begin{vvtm}
2497 %% \begin{ccode}
2498 %% record Time {
2499 %% year : Nat
2500 %% month : Nat
2501 %% day : Nat
2502 %% hour : Nat
2503 %% minute : Nat
2504 %% }
2505 %% \end{ccode}
2506 %% \end{vvtm}
2507 %% Here is another example, which needs slot renaming.
2508 %% \begin{vvtm}
2509 %% \begin{ccode}
2510 %% record Week {
2511 %% day : Nat
2512 %% }
2513 %% record Time'
2514 %% [ Date * ( day -> day-of-the-month ), Week * ( day -> day-of-the-week ) ]
2515 %% {
2516 %% hour : Nat
2517 %% minute : Nat
2518 %% }
2519 %% \end{ccode}
2520 %% \end{vvtm}
2521 %% Since \verb|day| was the slot name of both \verb|Date| and \verb|Week|,
2522 %% each slot was accorded a more accurate, distinct name.
2523
2524 \chapter{Axioms}\label{sec:p2-axioms}
2525
2526 \cafeobj~inherits the essences of algebraic specification techniques,
2527 where axioms are given as equations. Several extensions, however,
2528 lead to the following complications. (1) Equations may be conditional.
2529 (2) Not only equations, but transitions are available.
2530 (3) Visible sorts and hidden sorts demand different kinds of equivalences,
2531 so that, as well as ordinary equations, behavioural equations are
2532 available. All of these extensions are orthogonal, as witnessed by
2533 $8 = 2^3$ syntatical constructs.
2534
2535 \section{Variable Declaration}\label{sec:p2-variable-decl}
2536
2537 Axioms require representations of classes of elements, as well as
2538 specific elements. You have to be able to say, e.g., that
2539 the successor of {\em any} natural number is greater than that number.
2540 A {\em variable}\index{variable} represents this ``any''.
2541 In \cafeobj, every variable belongs to a sort, and represents an arbitrary
2542 term of that sort.
2543 Variables are declared as follows.
2544
2545 \begin{bsyntax} variable declaration \index{\texttt{var}}\index{\texttt{vars}} \Hline
2546 \texttt{var} \textit{ariable\_name} ``\texttt{:}'' \textit{sort\_name}
2547 \texttt{vars} \textit{list\_of\_variable\_names} ``\texttt{:}'' \textit{sort\_name}
2548 \end{bsyntax}
2549
2550 A variable name is a character string, and a list of variable names
2551 is blank-separated. Here is an example.
2552 \begin{vvtm}
2553 \begin{ccode}
2554 var N : Nat
2555 vars A B C : Nat
2556 \end{ccode}
2557 \end{vvtm}
2558 \begin{warning}
2559 In former versions, \verb|var| and \verb|vars| are interchangeable.
2560 This version becomes linguistically more strict.
2561 \verb|var| is for exactly one variable, while \verb|vars| is for
2562 one or more variables.
2563 \end{warning}
2564
2565 \begin{warning}
2566 In former versions, commas could be used as separators, so that the
2567 above declaration may be replaced with
2568 \begin{vvtm}
2569 \begin{ccode}
2570 vars A, B, C : Nat
2571 \end{ccode}
2572 \end{vvtm}
2573 But this construct no longer works: the system would regard \verb|,| as
2574 another variable.
2575 \end{warning}
2576
2577 A simple way to know the typing discipline of a language is to see
2578 variable declarations. \cafeobj~adopts a {\em strong typing}
2579 \index{strong typing} discipline,
2580 in that each variable has a syntactically determined type (sort). Another
2581 point of view comes from the definition of well-formedness. A term
2582 is well-formed iff it has a unique least parse
2583 (Section \ref{sec:p2-wff-terms}). A variable is a term.
2584 Hence it must have a unique least sort, the sort given in the
2585 declaration.
2586
2587 The scope of a variable is within the module where it is declared, or
2588 more precisely, in the rest of the module after the declaration.
2589 For example,
2590 \begin{vvtm}
2591 \begin{ccode}
2592 module M {
2593 [ S ]
2594 op f : S -> S
2595 -- axioms using X -- a.
2596 var X : S
2597 -- axioms using X -- b.
2598 }
2599 \end{ccode}
2600 \end{vvtm}
2601 would be an error, if you put axioms in \verb|a.| involving \verb|X|.
2602 The correct place for such axioms is \verb|b.|
2603
2604 It is illegal to declare a variable of the same name for different sorts.
2605 (And it is meaningless to re-declare the variable for the same sort.)
2606 Sometimes it is
2607 % b.c.b.g.\footnote{
2608 % Bon chic, bon genre.
2609 % }
2610 convenient
2611 to use common variable names, such as
2612 \verb|X|, for different sorts. And sometimes it is economical to
2613 use an ephemeral variable, valid only to the place where it appears.
2614 For these reasons, another construct for variable declaration is available.
2615
2616 \begin{bsyntax} variable declaration on the fly \Hline
2617 \textit{variable\_name}``\texttt{:}''\textit{sort\_name}
2618 \end{bsyntax}
2619
2620 \begin{warning}
2621 Do not insert blanks on either side of the colon.
2622 \begin{vvtm}
2623 \begin{ccode}
2624 N: Nat
2625 \end{ccode}
2626 \end{vvtm}
2627 or
2628 \begin{vvtm}
2629 \begin{ccode}
2630 N :Nat
2631 \end{ccode}
2632 \end{vvtm}
2633 is unacceptable. Only \verb|N:Nat| will do.
2634 \end{warning}
2635
2636 The following session illustrates the utility of declarations of
2637 this form. Recall from Section \ref{exs:simple-int} a module called
2638 \verb|SIMPLE-INT|.
2639 \begin{vvtm}
2640 \begin{ccode}
2641 CafeOBJ> parse in SIMPLE-INT : X:Int - s(0) .
2642
2643 (X - s(0)) : Int
2644 CafeOBJ>
2645 \end{ccode}
2646 \end{vvtm}
2647 The variable \verb|X| was declared on the fly. If you only want
2648 to check syntactic ambiguity, it is a bother to declare
2649 variables separately, just for that purpose.
2650 The system treats an ephemeral variable declaration as a term ---
2651 nothing more, nothing less ---,
2652 so you can declare on the fly wherever you can write a term.
2653 For the exact scope of on-the-fly declarations, see the next
2654 section.
2655
2656 \section{Equation Declaration}
2657
2658 \subsection{Unconditional Equation Declaration}\label{sec:p2-equation-decl}
2659
2660 A plain equation is an axiom. Anticipating conditional cases,
2661 we call it {\em unconditional}.\index{unconditional equation}
2662
2663 \begin{bsyntax} unconditional equation declaration \index{\texttt{eq}} \Hline
2664 \index{\texttt{=} (in equations)}
2665 \texttt{eq} $[$\textit{label}$]$ \textit{term} ``\texttt{=}'' \textit{term} ``\texttt{.}''
2666 \end{bsyntax}
2667
2668 The last period, preceded by a blank, is a must. It is the way to
2669 notify the system of the end of a term. ``\verb|=|'' separates
2670 the lefthand side from the righthand.
2671
2672 \begin{warning}
2673 ``\verb|=|'' in equations is used by the system to detect the
2674 end (and start) of a term. You should supply blanks on both
2675 ends, as in
2676 \begin{vvtm}
2677 \begin{ccode}
2678 eq X + 0 = X .
2679 \end{ccode}
2680 \end{vvtm}
2681 Without blanks, the system thinks the lefthand side is continuing.
2682 For example,
2683 \begin{vvtm}
2684 \begin{ccode}
2685 eq X + 0= X .
2686 \end{ccode}
2687 \end{vvtm}
2688 causes the system to treat the entire text after \verb|eq| (and
2689 the subsequent text, until it encounters a lonely ``\verb|=|'') as
2690 the term on the lefthand side.
2691 \end{warning}
2692
2693 Since an equation is a statement that the two sides are equal, they
2694 must belong to a common universe of discourse. For example, to say that
2695 Marie Antoinette equals 3.1416 is nonsense\footnote{
2696 Or are they both pie?
2697 }.
2698 In many-sorted logic, this consideration is formalised as
2699 ``two terms must be of the same sort''. \cafeobj, which is based on
2700 order-sorted logic, is more accommodating:
2701 \begin{quote}
2702 The sorts of the terms must belong to the same connected component
2703 (in the graph defined by the sort ordering).
2704 \end{quote}
2705 Accordingly, $1$ may be equal to $1.0$, to $2/2$, to $\sqrt 1$,
2706 and to $0i + 1$ (under the usual number hierarchy). We also require
2707 that
2708 \begin{quote}
2709 The terms be of visible sorts.
2710 \end{quote}
2711 For terms of hidden sorts, behavioural equations
2712 (Section \ref{sec:p2-beh-eq-decl}) should be used.
2713
2714 Labels do not affect the semantics, and may be omitted. If not
2715 omitted, it must be of the form
2716
2717 \begin{bsyntax} label in axioms \Hline
2718 ``\/\texttt{[}'' \textit{label\_name} ``\texttt{]}'' ``\texttt{:}''
2719 \end{bsyntax}
2720
2721 where a label name is a character string without blanks.
2722
2723 Since most non-trivial equations involve variables, and since
2724 unambiguous parsing is a non-trivial question, in examples below
2725 we put variable declarations near equation declarations.
2726
2727 To state that \verb|0| is a right identity of addition, you may write
2728 \begin{vvtm}
2729 \begin{ccode}
2730 var N : Nat
2731 eq [ r-id ] : N + 0 = N .
2732 \end{ccode}
2733 \end{vvtm}
2734 or, using an on-the-fly declaration,
2735 \begin{vvtm}
2736 \begin{ccode}
2737 eq [ r-id ] : N:Nat + 0 = N .
2738 \end{ccode}
2739 \end{vvtm}
2740 Look at the second occurrence of \verb|N| in this equation. In general,
2741 the scope of an on-the-fly declaration is the whole enclosing
2742 term (or more precisely, in the rest of the term).
2743 For example, the second \verb|N| of
2744 \begin{vvtm}
2745 \begin{ccode}
2746 N:Nat + s(N)
2747 \end{ccode}
2748 \end{vvtm}
2749 is the same \verb|N| as declared on the fly, since
2750 they are both in the same term. As a special rule, a variable
2751 declared within an axiom is valid throughout that axiom. The
2752 above equation illustrates this special rule.
2753
2754 On-the-fly declarations may overwrite the previous declarations.
2755 For example, in
2756 \begin{vvtm}
2757 \begin{ccode}
2758 module S {
2759 [ S T ]
2760 op f : S -> S
2761 var X : T
2762 eq f(f(X:S)) = f(X) .
2763 }
2764 \end{ccode}
2765 \end{vvtm}
2766 the variable \verb|X| was declared as of sort \verb|T|,
2767 but re-declared on the fly as of sort \verb|S|. Remember that
2768 overwriting variable declarations {\em terra firma} are illegal
2769 (Section \ref{sec:p2-variable-decl}). On-the-fly declarations
2770 are not subject to this prohibition. Moreover, on-the-fly
2771 declarations may themselves be overwritten.
2772 The following example uses \verb|X| first as of sort \verb|S|,
2773 then as of \verb|T|.
2774 \begin{vvtm}
2775 \begin{ccode}
2776 module S {
2777 [ S T ]
2778 op f : S T -> T
2779 eq f(X:S,X:T) = X .
2780 }
2781 \end{ccode}
2782 \end{vvtm}
2783
2784 \begin{warning}
2785 In spite of the above remark, it is advisable not to
2786 overwrite on-the-fly's by on-the-fly's. It would easily
2787 lead to confusion. Accordingly, the system gives a warning when
2788 it encounters such cases.
2789 \end{warning}
2790
2791 \subsection{Conditional Equation Declaration}\label{sec:p2-eq-decl-cond}
2792
2793 An equation may be {\em conditional}.\index{coditional equation}
2794 A conditional equation is of the form
2795
2796 \begin{bsyntax} conditional equation declaration \index{\texttt{ceq}} \Hline
2797 \index{\texttt{if}}
2798 \texttt{ceq} $[$\textit{label}$]$ \textit{term} ``\texttt{=}'' \textit{term} \texttt{if} \textit{boolean\_term} ``\texttt{.}''
2799 \end{bsyntax}
2800
2801 Labels and terms are as in \verb|eq|.
2802 As stated often enough, the last blank-period lets the system know the
2803 end of a term, and is a must. Note that a declaration starts with
2804 \verb|ceq|, not \verb|eq|. \verb|ceq| may be abbreviated to \verb|cq|.
2805 \index{\texttt{cq}}
2806
2807 \begin{warning}
2808 Like the last period, ``\verb|=|'' and \verb|if| demarcate terms. You
2809 should put blanks around them.
2810 \end{warning}
2811
2812 \begin{warning}
2813 In former versions, conditions were preceded by \verb|:if|, not \verb|if|.
2814 \end{warning}
2815
2816 A boolean term\index{boolean term} is a term of sort \verb|Bool|,
2817 which is declared in
2818 a built-in module called \verb|BOOL| (to be more precise, in a built-in
2819 module imported by \verb|BOOL|)\index{\texttt{BOOL}}.
2820 A conditional equation states that the two terms are equal if
2821 the boolean term is true (i.e. it equals the constant \verb|true|).
2822 We may say, therefore, that a boolean term {\em is} a condition.
2823 \index{condition (of an equation)} As mentioned earlier
2824 (Section \ref{sec:p2-import-decl}), every
2825 module implicitly imports the module \verb|BOOL|, unless you have locked
2826 it out. A major reason is that \verb|BOOL| provides you with boolean terms.
2827 \verb|BOOL| contains all the usual boolean operators, so that,
2828 as conditions, the following terms may be used
2829 (if \verb|B|, \verb|B'| are boolean terms).
2830 \begin{vvtm}
2831 \begin{ccode}
2832 true
2833 false
2834 not B
2835 B and B'
2836 B or B'
2837 \end{ccode}
2838 \end{vvtm}
2839 A complete listing may be obtained by \verb|show| command
2840 (see Section \ref{sec:p2-inspect-module}).
2841
2842
2843 \subsection{Behavioural Equation Declaration}\label{sec:p2-beh-eq-decl}
2844
2845 To state behavioural equality explicitly, the following constructs are
2846 available.
2847
2848 \begin{bsyntax} behavioural equation declaration \Hline
2849 \index{behavioural equation}\index{\texttt{beq}}\index{\texttt{bceq}}
2850 \texttt{beq} $[$\textit{label} $]$ \textit{term} ``\texttt{=}'' \textit{term} ``\texttt{.}''
2851 \texttt{bceq} $[$\textit{label}$]$ \textit{term} ``\texttt{=}'' \textit{term} \texttt{if} \textit{boolean\_term} ``\texttt{.}''
2852 \end{bsyntax}
2853
2854 \verb|bceq| may be abbreviated to \verb|bcq|.\index{\texttt{bcq}}
2855 Labels etc. are as in \verb|eq| or \verb|ceq|. Also as in \verb|eq|
2856 or \verb|ceq|, both terms must belong to the same connected
2857 component.
2858 %In addition, we require that
2859 %\begin{quote}
2860 % Any operator in the terms, if it has a hidden sort in its arity,
2861 % be behavioural
2862 %\end{quote}
2863 %i.e. it should have been declared with \verb|bop|, not with \verb|op|
2864 %(Sections \ref{sec:p2-op-decl}, \ref{sec:p2-beh-op-decl}).
2865 %For example, if \verb|S| is a hidden sort and \verb|T| a visible one,
2866 %\begin{vvtm}
2867 %\begin{ccode}
2868 % bop f : S -> T
2869 % bop g : S T -> S
2870 %\end{ccode}
2871 %\end{vvtm}
2872 %are allowed since they are declared behavioural;
2873 %\begin{vvtm}
2874 %\begin{ccode}
2875 % op c : -> T
2876 % op d : -> S
2877 % op h : T -> T
2878 % op k : T -> S
2879 %\end{ccode}
2880 %\end{vvtm}
2881 %are also allowed, since they do not act on \verb|S|; but
2882 %\begin{vvtm}
2883 %\begin{ccode}
2884 % op u : S -> T
2885 % op v : S T -> T
2886 %\end{ccode}
2887 %\end{vvtm}
2888 %are prohibited, since they act on \verb|S| but are not declared bahavioural.
2889
2890 \begin{warning}
2891 According to the above definition, you can write behavioural equations over
2892 visible sort. There is no harm in that: such equations are treated
2893 just like equations with \verb|eq| or \verb|ceq|.
2894 But do avoid using \verb|beq| if \verb|eq| is what you mean.
2895 Legality does not imply good manners.
2896 \end{warning}
2897
2898 Note that in many cases, behavioural equality is defined, indirectly,
2899 with ordinary equations (declared with \verb|eq| or \verb|ceq|), not with
2900 behavioural ones (cf. Section \ref{exs:counter}).
2901 Behavioural equations are reserved for cases
2902 where explicit statements are desirable: typically, when
2903 you state theorems, or want to define behavioural operators in terms of
2904 others.
2905
2906 \section{Transition Declaration}\label{sec:p2-trans-decl}
2907
2908 Equality is an equivalence relation, obeying three basic properties
2909 of binary relations: reflexivity, symmetry, and transitivity.
2910 Dropping one of these properties engenders interesting relations.
2911 Partial equivalence is one of them. By dropping symmetry, we get
2912 a {\em transition}\index{transition} relation.
2913
2914 The following constructs define such a transition relation.
2915 As with equations, there are conditional and unconditional transitions,
2916 and ordinary and behavioural ones. Crossing these, you get
2917
2918 \begin{bsyntax} transition declaration \Hline
2919 \index{\texttt{trans}}\index{\texttt{ctrans}}\index{\texttt{btrans}}
2920 \index{\texttt{bctrans}}\index{\texttt{=>} (in transitions)}
2921 \texttt{trans} $[$\textit{label}$]$ \textit{term} ``\texttt{=>}'' \textit{term} ``\texttt{.}''
2922 \texttt{ctrans} $[$\textit{label}$]$ \textit{term} ``\texttt{=>}'' \textit{term} \texttt{if} \textit{boolean\_term} ``\texttt{.}''
2923 \texttt{btrans} $[$\textit{label}$]$ \textit{term} ``\texttt{=>}'' \textit{term} ``\texttt{.}''
2924 \texttt{bctrans} $[$\textit{label}$]$ \textit{term} ``\texttt{=>}'' \textit{term} \texttt{if} \textit{boolean\_term} ``\texttt{.}''
2925 \end{bsyntax}
2926
2927 Syntactically, transitions are the same as equations, if you
2928 replace the equality sign by the arrow ``\verb|=>|''. Similar restrictions
2929 apply here also. For example, the sorts of two terms must be in the same
2930 connected component.
2931 %, and terms in behavioural transitions should not
2932 %contain non-behavioural operators on hidden sorts.
2933 \verb|trans| may be abbreviated to \verb|trns|,
2934 \verb|ctrans| to \verb|ctrns|, \verb|btrans| to \verb|btrns|, and
2935 \verb|bctrans| to \verb|bctrns|.
2936 \index{\texttt{trns}}\index{\texttt{ctrns}}\index{\texttt{btrns}}\index{\texttt{bctrns}}
2937
2938 The following little example illustrates the difference between equations
2939 and transitions.\label{exs:choice}
2940 \begin{vvtm}
2941 \begin{ccode}
2942 module CHOICE {
2943 [ State ]
2944 ops a b : -> State
2945 op _|_ : State State -> State
2946 vars X Y : State
2947 trans X | Y => X .
2948 trans X | Y => Y .
2949 }
2950 \end{ccode}
2951 \end{vvtm}
2952 ``\verb+_|_+'' denotes an indeterminate state, turning out to be either
2953 argument (which itself may be indeterminate). The above rules
2954 say that an indeterminate state may become more and more
2955 determinate, but not conversely. It is obvious that,
2956 if these rules were equations, all the states would be the same, so
2957 that \verb|State| would be an uninteresting singleton.
2958
2959 A traditional way to describe the above choice operator is to
2960 define an explicit transition function. The technique works at times,
2961 but in this nondeterministic example, it would lead to something like
2962 \begin{vvtm}
2963 \begin{ccode}
2964 module CHOICE {
2965 [ State ]
2966 ops a b : -> State
2967 op _|_ : State State -> State
2968 op choose : State -> State
2969 vars X Y : State
2970 eq choose(X | Y) = X .
2971 eq choose(X | Y) = Y .
2972 eq choose(a) = a .
2973 eq choose(b) = b .
2974 }
2975 \end{ccode}
2976 \end{vvtm}
2977 which, alas, is a specification of singletons.
2978
2979 \section{Internalising Axioms}\label{sec:p2-internal-axioms}
2980
2981 Axioms define certain kinds of relations when we construct models.
2982 For example, axioms introduced by \verb|eq| or \verb|ceq| define
2983 minimal congruence relations between reachable (term-generated)
2984 elements of visible sorts. These relations reside outside of \cafeobj
2985 ~descriptions:
2986 the mark ``\verb|=|'' is just a notation.
2987
2988 For various purposes, it is convenient if these relations can be
2989 manipulated within the syntactical framework of \cafeobj. In general,
2990 it is impossible to give general decision procedures for these
2991 relations, but under certain conditions, or in some restricted
2992 sense, it is possible to implement procedures that realise or
2993 surrogate these relations. \cafeobj~provides four predicates
2994 for this purpose.
2995 \begin{itemize}
2996 \item \verb|==| for equality.
2997 \item \verb|==>| for transition relations.
2998 \item \verb|=*=|, \verb|=b=| for behavioural equivalence.
2999 \end{itemize}
3000 The latter three are mainly used in theorem proving
3001 (cf. Section \ref{sec:p2-theorem-proving-exs}).
3002
3003 In addition, the system supports a sort predicate ``\verb|:is|''.
3004
3005 \subsection{Equality Predicate}\label{sec:p2-equality-pred}
3006
3007 The predicate for (ordinary) equality, written ``\verb|==|'',\index{\texttt{==}}
3008 is a binary operator defined on each visible sort.
3009 An explicit declaration of the operator would be of the form
3010 \begin{vvtm}
3011 \begin{ccode}
3012 op _==_ : S S -> Bool
3013 \end{ccode}
3014 \end{vvtm}
3015 or
3016 \begin{vvtm}
3017 \begin{ccode}
3018 pred _==_ : S S
3019 \end{ccode}
3020 \end{vvtm}
3021 for each sort \verb|S|. ``\verb|_==_|'' is defined in terms of
3022 evaluation (Chapter \ref{sec:p2-term-evaluation}).
3023 For ground terms \verb|t|, \verb|t'| of sort \verb|S|,
3024 ``\verb|t == t'|'' equals \verb|true| iff \verb|t|, \verb|t'| evaluate to
3025 a common term. It takes care of equational theories,
3026 such as associativity (Section \ref{sec:p2-equational-theory}). Due to this
3027 definition, ``\verb|_==_|'' works correctly only if the
3028 term rewriting system\index{term rewriting system}
3029 is terminating and
3030 confluent (Section \ref{sec:p2-executable-module}).
3031
3032 The converse of ``\verb|_==_|'' is ``\verb|_=/=_|'',\index{\texttt{=/=}}
3033 which returns
3034 \verb|true| iff ``\verb|_==_|'' returns \verb|false| on the same arguments.
3035
3036 The exact mechanism for defining these operators is tricky, but all
3037 are declared and defined in a built-in module \verb|BOOL|.
3038 This module contains usual boolean operators, such as conjunction,
3039 and, as mentioned earlier, is implicitly imported by other modules
3040 (Section \ref{sec:p2-import-decl}).
3041
3042 Note the difference between equations, such as
3043 \begin{vvtm}
3044 \begin{ccode}
3045 eq N + 0 = N .
3046 \end{ccode}
3047 \end{vvtm}
3048 and applications of the {\em operator} ``\verb|_==_|'', such as
3049 \begin{vvtm}
3050 \begin{ccode}
3051 N + 0 == N
3052 \end{ccode}
3053 \end{vvtm}
3054 The sign ``\verb|=|'' in the former {\em denote\/}s an equivalence relation,
3055 and this relation per se steer wide of syntactic manipulation.
3056 ``\verb|_==_|'' is an operator that generates terms, which are subject to
3057 manipulation within the language processors. Still, they are related as
3058 follows:
3059 if the underlying term rewriting system is confluent and terminating,
3060 \begin{quotation}
3061 \verb|X = X'| holds iff \verb|X == X' = true| does.
3062 \end{quotation}
3063
3064 %There is another massively polymorphic equality predicate, which,
3065 %unlike ``\verb|_==_|'', does not evaluate arguments.
3066 %Its name is ``\verb|_===_|''. This operator is declared and defined in
3067 %a built-in module called \verb|IDENTICAL|. The converse is
3068 %``\verb|_=/==_|''.
3069 %
3070 %\begin{warning}
3071 % At the time of writing, the authors do not know why these two
3072 % operators are necessary or useful. These came from OBJ3.
3073 %\end{warning}
3074
3075 \subsection{Transition Predicate}\label{sec:p2-transition-pred}
3076
3077 The predicate for transition relation, written ``\verb|==>|'',
3078 \index{\texttt{==>}} is a binary
3079 operator defined on each visible sort. As with ``\verb|==|'' in the
3080 previous section, an explicit declaration would be of the form
3081 \begin{vvtm}
3082 \begin{ccode}
3083 pred _==>_ : S S
3084 \end{ccode}
3085 \end{vvtm}
3086 for each sort \verb|S|. This predicate is in fact declared on
3087 the ``universal'' sort, in a module called \verb|RWL|.
3088 \verb|RWL| is imported implicitly, when a module contains
3089 transition declarations (Section \ref{sec:p2-trans-decl}).
3090 As with \verb|BOOL| (Section \ref{sec:p2-import-decl}), it is
3091 possible to switch off this implicit importation, by the command
3092 \index{\texttt{set}}\index{\texttt{include}}\index{\texttt{RWL}}
3093 \begin{vvtm}
3094 \begin{ccode}
3095 CafeOBJ> set include RWL off
3096 \end{ccode}
3097 \end{vvtm}
3098
3099 A transition relation is reflexive, transitive, and is closed under
3100 operator application. When \verb|RWL| is imported, the system
3101 supplies a set of axioms.
3102 Firstly, for each visible sort \verb|S|, an axiom
3103 \begin{vvtm}
3104 \begin{ccode}
3105 eq X:S ==> X = true .
3106 \end{ccode}
3107 \end{vvtm}
3108 is declared. (In fact, a single equation declared in \verb|RWL| works
3109 for any \verb|S|.) This axiom states reflexivity.
3110 Secondly, for each unconditional tranision
3111 \begin{vvtm}
3112 \begin{ccode}
3113 trans t => t' .
3114 \end{ccode}
3115 \end{vvtm}
3116 the system declares
3117 \begin{vvtm}
3118 \begin{ccode}
3119 eq t ==> t' = true .
3120 \end{ccode}
3121 \end{vvtm}
3122 and for each conditional transition
3123 \begin{vvtm}
3124 \begin{ccode}
3125 ctrans t => t' if c .
3126 \end{ccode}
3127 \end{vvtm}
3128 the system declares
3129 \begin{vvtm}
3130 \begin{ccode}
3131 ceq t ==> t' = true if c .
3132 \end{ccode}
3133 \end{vvtm}
3134 These declarations correspond to one-step transitions.
3135
3136 Thirdly, for each operator declaration
3137 \begin{vvtm}
3138 \begin{ccode}
3139 op f : S1 ... Sn -> S
3140 \end{ccode}
3141 \end{vvtm}
3142 the system declares
3143 \begin{vvtm}
3144 \begin{ccode}
3145 ceq f(X1:S1, ..., Xn:Sn) ==> f(X1':S1, ..., Xn':Sn) = true
3146 if X1 ==> X1' and ... and Xn ==> Xn' .
3147 \end{ccode}
3148 \end{vvtm}
3149 which states that ``\verb|_==>_|'' is closed under \verb|f|'s
3150 application.
3151
3152 As to transitivity, a straightforward declaration would not do:
3153 an axiom of the form
3154 \begin{vvtm}
3155 \begin{ccode}
3156 ceq X:S ==> X':S = true if X ==> X'':S and X'' ==> X' .
3157 \end{ccode}
3158 \end{vvtm}
3159 introduces an extra variable in the condition, which makes it
3160 inappropriate for the purpose of evaluation (cf. Section
3161 \ref{sec:p2-executable-module}). Considering that the main usage of
3162 the predicate ``\verb|==>|'' is to prove theorems with evaluation mechanism,
3163 this fact renders such an axiom useless. In its place the system add
3164 the following axiom.
3165 \begin{vvtm}
3166 \begin{ccode}
3167 ceq X:S ==> X':S = true if X =(*)=> X' .
3168 \end{ccode}
3169 \end{vvtm}
3170 Attached to the operator ``\verb|_=(*)=>_|'' is a systematic search
3171 procedure for determining whether the first argument transits to
3172 the second in an arbitraty number of steps. More on this and related
3173 operators in Section \ref{sec:p2-proving-trans-2}.
3174
3175 \subsection{Behavioural Equivalence Predicate}\label{sec:p2-beh-equiv-pred}
3176
3177 The predicate for behavioural equivalence, written ``\verb|=*=|'',
3178 \index{\texttt{=*=}} is a binary
3179 operator defined on each hidden sort. As with ``\verb|==|'' and
3180 ``\verb|==>|'' in previous sections, an explicit declaration would be of the
3181 form
3182 \begin{vvtm}
3183 \begin{ccode}
3184 pred _=*=_ : S S
3185 \end{ccode}
3186 \end{vvtm}
3187 for a given sort \verb|S|.
3188
3189 Remember that behavioural operators are declared with \verb|bop|, and each
3190 such operator has exactly one hidden sort in its arity
3191 (Section \ref{sec:p2-beh-op-decl}).
3192 On the assumption that ``\verb|a =*= a'|'' implies
3193 ``\verb|f1(a) == f1(a')|'' for any ``attribute'' \verb|f| and for
3194 any \verb|a|, if ``\verb|=*=|'' turns out to be congruent wrt all the
3195 ``method''s, the system declares an axiom
3196 \begin{vvtm}
3197 \begin{ccode}
3198 eq A =*= A' = f1(A) == f1(A') and ... and fn(A) == fn(A') .
3199 \end{ccode}
3200 \end{vvtm}
3201 where \verb|f1|, \ldots, \verb|fn| are the attributes.
3202
3203 Recalling an example of bank accounts
3204 \begin{vvtm}
3205 \begin{ccode}
3206 *[ Account ]*
3207 bop deposit : Account Nat -> Account
3208 bop withdraw : Account Nat -> Account
3209 bop balance : Account -> Nat
3210 \end{ccode}
3211 \end{vvtm}
3212 the above assumption is
3213 \begin{vvtm}
3214 \begin{ccode}
3215 ceq balance(A:Account) = balance(A':Account) if A =*= A' .
3216 \end{ccode}
3217 \end{vvtm}
3218 and ``\verb|=*=|'' is congruent if two equations
3219 \begin{vvtm}
3220 \begin{ccode}
3221 eq deposit(a,n) =*= deposit(a',n) = true .
3222 eq withdraw(a,n) =*= withdraw(a',n) = true .
3223 \end{ccode}
3224 \end{vvtm}
3225 hold, on the hypothesis that
3226 \begin{vvtm}
3227 \begin{ccode}
3228 eq a =*= a' = true .
3229 \end{ccode}
3230 \end{vvtm}
3231 for arbitrary \verb|a|, \verb|a'|, and \verb|n|.
3232
3233 If the switch ``\kbd{accept} \verb|=*=| \kbd{proof}'' is \kbd{on}
3234 (the default is \kbd{off}),
3235 the system tries to prove these two equations
3236 by evaluation (Chapter \ref{sec:p2-term-evaluation}).
3237 If ``\verb|_=*=_|'' does turn out to be a congruence, the system
3238 adds the axiom
3239 \begin{vvtm}
3240 \begin{ccode}
3241 ceq A:Account =*= A':Account if balance(A) == balance(A') .
3242 \end{ccode}
3243 \end{vvtm}
3244 Intuitively, this axiom states that two terms of \verb|Account| are
3245 indistinguishable if they are indistuiguishable by a single
3246 application of \verb|balance|.
3247
3248 Note that, if ``\verb|=*=|'' is not turned out to be congruent,
3249 no axiom defines ``\verb|=*=|''. In such a case, ``\verb|=*=|''
3250 is not even reflexive.
3251
3252 %\begin{warning}
3253 %The existence of completely undefined ``\verb|=*=|'' is annoying.
3254 %In future, this predicate may be defined as above, whether or not
3255 %it is congruent.
3256 %\end{warning}
3257
3258 Another predicate for behavioural equivalence is ``\verb|=b=|,''
3259 \index{\texttt{=b=}}
3260 which is to behavioural equations as ``\verb|==|'' is to plain ones, and is
3261 defined to \verb|true| iff both hand sides evaluate to a common
3262 term. The difference between ``\verb|==|'' and ``\verb|=b=|''
3263 is in the applicability of behavioural axioms
3264 (See \ref{sec:p2-evaluation-command} for detail).
3265
3266 \subsection{Sort Predicate}\label{sec:p2-sort-pred}
3267
3268 The system supplies a predicate that checks whether a given
3269 term is of a given sort. It is of the form
3270 \begin{vvtm}
3271 \begin{ccode}
3272 t :is S
3273 \end{ccode}
3274 \end{vvtm}
3275 and evaluates to \verb|true| if \verb|t| belongs to \verb|S|.
3276 You can supply explicit axioms for the predicate. An example is
3277 \begin{vvtm}
3278 \begin{ccode}
3279 vars N N' : Nat
3280 ceq N + N' :is NzNat = true if N :is NzNat .
3281 ceq N + N' :is NzNat = true if N' :is NzNat .
3282 \end{ccode}
3283 \end{vvtm}
3284
3285 \begin{warning}
3286 This feature is introduced as a surrogate of membership relation.
3287 We stress that it is a surrogate, not a real thing.
3288 % The existence (and implementation) of this predicate is highly
3289 % tentative, and maybe dropped entirely at any time.
3290 You should not use this feature unless absolutely necessary.
3291 There are switches such as ``\verb|mel sort|'' related to
3292 this predicate. They are also highly tentative, and you should not
3293 use them unless absolutely necessary.
3294 \end{warning}
3295
3296 \chapter{Inspecting Modules}\label{sec:p2-inspect-module}
3297
3298 It is time we show some examples that combine all of the
3299 declarations explained so far. This chapter also illustrates
3300 the module inspection command \verb|show| (Section \ref{sec:p2-show-command}).
3301
3302 \section{Using Inspection Commands}\label{sec:p2-insp-basic}
3303
3304 \subsection{Printing Whole Modules}\label{sec:p2-print-whole-module}
3305
3306 We first recall \verb|SIMPLE-NAT|, which defines natural number addition
3307 (Section \ref{exs:simple-nat}).
3308 \begin{vvtm}
3309 \begin{ccode}
3310 module SIMPLE-NAT {
3311 [ Zero NzNat < Nat ]
3312 op 0 : -> Zero
3313 op s : Nat -> NzNat
3314 op _+_ : Nat Nat -> Nat
3315 vars N N' : Nat
3316 eq 0 + N = N .
3317 eq s(N) + N' = s(N + N') .
3318 }
3319 \end{ccode}
3320 \end{vvtm}
3321 Everything in this module has been already explained, so you should
3322 know that this is indeed a standard recursive definition of addition.
3323 A subtle point is in the use of subsort relation. Here the set of
3324 natural numbers is the union of \verb|Zero|, which contains \verb|0|
3325 (and nothing else), and \verb|NzNat|, which contains
3326 \verb|s(0)|, \verb|s(s(0))|, and so forth.
3327
3328 Suppose this definition is in the file \verb|simple-nat.cafe|. Then
3329 you may get the following session.
3330 \begin{vvtm}
3331 \begin{ccode}
3332 CafeOBJ> input simple-nat
3333
3334 -- processing input : ./simple-nat.cafe
3335 -- defining module SIMPLE-NAT....._..* done.
3336 CafeOBJ> show SIMPLE-NAT
3337 module SIMPLE-NAT {
3338 imports {
3339 protecting (BOOL)
3340 }
3341 signature {
3342 [ Nat, Zero NzNat < Nat,
3343 NzNat, NzNat < Nat,
3344 Zero, Zero < Nat ]
3345 op 0 : -> Zero
3346 op s : Nat -> NzNat
3347 op _ + _ : Nat Nat -> Nat { strat: (1 0 2) }
3348 }
3349 axioms {
3350 var N : Nat
3351 var N' : Nat
3352 eq 0 + N = N .
3353 eq s(N) + N' = s(N + N') .
3354 }
3355 }
3356
3357 CafeOBJ>
3358 \end{ccode}
3359 \end{vvtm}
3360 When invoked with a module name, \verb|show| command prints the entire
3361 contents of the module. You may notice that
3362 \begin{itemize}
3363 \item Block constructs \verb|imports|, \verb|signature|, and
3364 \verb|axioms| (Section \ref{sec:p2-blocks-in-mod}) are used,
3365 even though you did not use them.
3366 \item The module \verb|BOOL| is imported automatically. As explained
3367 earlier, \verb|BOOL| defines Boolean operators and is imported unless
3368 prohibited by a switch (Section \ref{sec:p2-import-decl}).
3369 \item There is a bracketed text after the declaration of ``\verb|_+_|''.
3370 This is an operator attribute, to be explained later
3371 (Section \ref{sec:p2-op-properties}).
3372 \end{itemize}
3373 \begin{warning}
3374 Display of sort declarations is often knotted. This is due to
3375 implementation restriction; an ``optimal'' display is very hard to
3376 define anyway.
3377 \end{warning}
3378
3379 \subsection{Print Part of Modules}\label{sec:p2-print-part-of-module}
3380
3381 To print partial contents, such as sort declarations only, of
3382 a module, you may supply further arguments to \verb|show| command.
3383 Continuing the above session, you get
3384 \begin{vvtm}
3385 \begin{ccode}
3386 CafeOBJ> show sorts SIMPLE-NAT
3387 * visible sorts :
3388 Zero, Zero < Nat
3389 NzNat, NzNat < Nat
3390 Nat, Zero NzNat < Nat
3391
3392 CafeOBJ> show ops SIMPLE-NAT
3393 ..................................(0)..................................
3394 * rank: -> Zero
3395 ..................................(s)..................................
3396 * rank: Nat -> NzNat
3397 ................................(_ + _)................................
3398 * rank: Nat Nat -> Nat { strat: (1 0 2) }
3399 - axioms:
3400 eq 0 + N = N
3401 eq s(N) + N' = s(N + N')
3402
3403 CafeOBJ>
3404 \end{ccode}
3405 \end{vvtm}
3406 The arguments \verb|sorts|, \verb|ops|, etc. specify the parts of
3407 the module to be shown. To summarise the mostly obvious,
3408 \begin{center}
3409 \begin{tabular}{ll}
3410 \verb|sorts| & sort and subsort declarations \\
3411 \verb|ops| & operator declarations \\
3412 \verb|vars| & variable declarations \\
3413 \verb|axioms| & axiom declarations \\
3414 \verb|params| & parameter declarations (to be introduced later) \\
3415 \verb|subs| & import declarations (for `sub'modules) \\
3416 \verb|sign| & \verb|sorts| and \verb|ops| combined (i.e. the signature) \\
3417 \end{tabular}
3418 \end{center}
3419
3420 It is possible to list declarations in imported modules also. For
3421 an example for example's sake,
3422 \begin{vvtm}
3423 \begin{ccode}
3424 CafeOBJ> module SHADOW-NAT {
3425 protecting (SIMPLE-NAT)
3426 }
3427
3428 -- defining module SHADOW-NAT._* done.
3429 CafeOBJ> show sorts SHADOW-NAT
3430
3431 CafeOBJ> show all sorts SHADOW-NAT
3432 * visible sorts :
3433 Zero, Zero < Nat
3434 NzNat, NzNat < Nat
3435 Nat, Zero NzNat < Nat
3436
3437 CafeOBJ>
3438 \end{ccode}
3439 \end{vvtm}
3440 \verb|SHADOW-NAT| simply imported declarations of \verb|SIMPLE-NAT|
3441 and added nothing. The plain \verb|sorts| argument prints nothing,
3442 while \verb|all sorts| prints sort declarations in the imported
3443 \verb|SIMPLE-NAT|.
3444
3445 \begin{warning}
3446 \verb|all| does not print anything in the implicitly imported modules.
3447 Even if you import \verb|BOOL| explicitly, the system still refuses to
3448 print the declarations in them.
3449 \end{warning}
3450
3451 In case of axioms, instead of crying \verb|all| to \verb|show| command,
3452 you may change a switch called ``\verb|all axioms|'', as
3453 \begin{vvtm}
3454 \begin{ccode}
3455 CafeOBJ> set all axioms on
3456 \end{ccode}
3457 \end{vvtm}
3458
3459 If you are inspecting a particular module, it is convient to set
3460 it as current (Section \ref{sec:p2-current-module}), so that the
3461 module name may be omitted.\index{current module}
3462 \begin{vvtm}
3463 \begin{ccode}
3464 CafeOBJ> select SIMPLE-NAT
3465
3466 SIMPLE-NAT> show sorts
3467 * visible sorts :
3468 Zero, Zero < Nat
3469 NzNat, NzNat < Nat
3470 Nat, Zero NzNat < Nat
3471
3472 SIMPLE-NAT> show ops
3473 ..................................(0)..................................
3474 * rank: -> Zero
3475 ..................................(s)..................................
3476 * rank: Nat -> NzNat
3477 ................................(_ + _)................................
3478 * rank: Nat Nat -> Nat
3479 - attributes: { strat: (1 0 2) }
3480 - axioms:
3481 eq 0 + N = N
3482 eq s(N) + N' = s(N + N')
3483
3484 SIMPLE-NAT>
3485 \end{ccode}
3486 \end{vvtm}
3487 Note that the prompt has changed, to indicate what module is current.
3488
3489 It is possible to focus on a single sort, operator or imported module.
3490 For example,
3491 \begin{vvtm}
3492 \begin{ccode}
3493 SIMPLE-NAT> show sort Nat
3494 Sort Nat declared in the module SIMPLE-NAT
3495 - subsort relation :
3496
3497 ?Nat
3498 |
3499 Nat
3500 / \
3501 Zero NzNat
3502
3503
3504
3505 SIMPLE-NAT> show op _+_
3506 ................................(_ + _)................................
3507 * rank: Nat Nat -> Nat
3508 - attributes { strat: (1 0 2) }
3509 - axioms:
3510 eq 0 + N = N
3511 eq s(N) + N' = s(N + N')
3512
3513 SIMPLE-NAT>
3514 \end{ccode}
3515 \end{vvtm}
3516 Here the subsort relation is shown graphically. \verb|?Nat| is
3517 an error sort, and you should ignore it for now (cf. Section
3518 \ref{sec:p2-flexible-typing}).
3519
3520 A switch controls whether the sort of each variable is displayed.
3521 \index{\texttt{set}}{\texttt{show}}\index{\texttt{var}}\index{\texttt{sorts}}
3522 Continuing the session, you get
3523 \begin{vvtm}
3524 \begin{ccode}
3525 SIMPLE-NAT> set show var sorts on
3526
3527 SIMPLE-NAT> show op _+_
3528 ................................(_ + _)................................
3529 * rank: Nat Nat -> Nat
3530 - attributes { strat: (1 0 2) }
3531 - axioms:
3532 eq 0 + N:Nat = N:Nat
3533 eq s(N:Nat) + N':Nat = s(N:Nat + N':Nat)
3534
3535 SIMPLE-NAT>
3536 \end{ccode}
3537 \end{vvtm}
3538 \begin{warning}
3539 This switch does not have a total control. In cases when sorts
3540 are not apparent --- when an axiom is from an imported module, or
3541 when variables are redeclared on the fly, and so on --- the system displays
3542 them willynilly.
3543 \end{warning}
3544
3545 \subsection{Deep Inspection}\label{sec:p2-deep-inspection}
3546
3547 If you want more detailed explanations of the module contents, use
3548 \verb|describe|, in place of \verb|show|.
3549 This command is useful when you are evaluating terms
3550 (Chapter \ref{sec:p2-term-evaluation}). Since we have not yet
3551 explained term evaluation mechanisms, you should not expect to
3552 understand the following output. It is enough to know the
3553 flavour of difference between \verb|show| and \verb|describe|.
3554 \begin{vvtm}
3555 \begin{ccode}
3556 SIMPLE-NAT> describe op _+_
3557 ======================================================================
3558 name : (_ + _)
3559 module : SIMPLE-NAT
3560 number of arguments : 2
3561 default attributes :
3562 rewrite strategy : not specified
3563 syntax :
3564 precedence : not specified
3565 computed prec. : 41
3566 form : (arg:41) "+" (arg:41)
3567 theory :
3568 ----------------------------------------------------------------------
3569 rank : ?Nat ?Nat -> ?Nat
3570 module : SIMPLE-NAT
3571 theory :
3572 rewrite strategy : (1 2 0)
3573 precedence : 41
3574 lower oprations :
3575 Nat Nat -> Nat
3576 axioms :
3577 ----------------------------------------------------------------------
3578 rank : Nat Nat -> Nat
3579 module : SIMPLE-NAT
3580 theory :
3581 rewrite strategy : (1 0 2 0)
3582 precedence : 41
3583 axioms :
3584 - equation
3585 lhs : 0 + N:Nat
3586 rhs : N:Nat
3587 top operator : Nat Nat -> Nat
3588 - equation
3589 lhs : s(N:Nat) + N':Nat
3590 rhs : s(N:Nat + N':Nat)
3591 top operator : Nat Nat -> Nat
3592
3593 SIMPLE-NAT>
3594 \end{ccode}
3595 \end{vvtm}
3596
3597 %% \newpage
3598 %% \subsection{Inspecting Records}\label{sec:p2-record-inspection}
3599
3600 %% As explained in Section \ref{sec:p2-plain-record-decl}, a record
3601 %% declaration has the same effects as several related declarations
3602 %% of sorts and operators. You may be anxious to know how the system
3603 %% handles record declarations. If you are, put the record \verb|Date|
3604 %% into a module declaration, and inspect it.\label{exs:date}
3605 %% \begin{vvtm}
3606 %% \begin{ccode}
3607 %% CafeOBJ> module DATE {
3608 %% protecting (SIMPLE-NAT)
3609 %% record Date {
3610 %% year : Nat
3611 %% month : Nat
3612 %% day : Nat
3613 %% } }
3614 %% -- defining module DATE.._* done.
3615 %% CafeOBJ> select DATE
3616 %% DATE> show sorts
3617 %% * visible sorts :
3618 %% Date, Date < RecordInstance
3619 %% RecordDate, RecordDate < RecordId
3620 %% Slotyear, Slotyear < AttrId
3621 %% Slotmonth, Slotmonth < AttrId
3622 %% Slotday, Slotday < AttrId
3623 %% DATE>
3624 %% \end{ccode}
3625 %% \end{vvtm}
3626 %% \verb|SIMPLE-NAT| (Section \ref{exs:simple-nat}) should be familiar.
3627 %% The record \verb|Date| was indeed declared as a sort, as we stated.
3628 %% But it seems that a lot more sorts are involved. They are used to define
3629 %% the operators. To see what operators were declared, you may inspect
3630 %% further. But hold your breath:
3631 %% \begin{vvtm}
3632 %% \begin{ccode}
3633 %% DATE> show ops
3634 %% ................................(_ { })................................
3635 %% * rank: RecordDate -> Date
3636 %% - attributes: { constr }
3637 %% ...............................(_ { _ })...............................
3638 %% * rank: RecordDate Attributes -> Date
3639 %% - attributes: { constr }
3640 %% ................................(Date)................................
3641 %% * rank: -> RecordDate
3642 %% - attributes: { constr }
3643 %% ..............................(makeDate)..............................
3644 %% * rank: Attributes -> Date
3645 %% - axioms:
3646 %% eq makeDate(ATTRS-6:Attributes) = #!! (make-record-instance 'date
3647 %% attrs-6)
3648 %% ..............................(makeDate)..............................
3649 %% * rank: -> Date
3650 %% - axioms:
3651 %% eq makeDate = #!! (make-record-instance 'date)
3652 %% ................................(year)................................
3653 %% * rank: -> Slotyear
3654 %% - attributes: { constr }
3655 %% ................................(year)................................
3656 %% * rank: Date -> Nat
3657 %% - axioms:
3658 %% eq year(RID-5:RecordDate { (VAR-SLOTYEAR-2:Slotyear = VAL-11:Nat,
3659 %% ATTRS-6:Attributes) }) = VAL-11:Nat
3660 %% ..............................(set-year)..............................
3661 %% * rank: Date Nat -> Date
3662 %% - axioms:
3663 %% eq set-year(RID-5:RecordDate { (VAR-SLOTYEAR-2:Slotyear = VAL-11:Nat,
3664 %% ATTRS-6:Attributes) },NEW-VAL-12) = RID-5:RecordDate
3665 %% { (VAR-SLOTYEAR-2:Slotyear = NEW-VAL-12 , ATTRS-6:Attributes) }
3666 %% \end{ccode}
3667 %% \end{vvtm}
3668 %% The print continues for \verb|month|, \verb|set-month| and so on.
3669 %% We do not explain the above in detail, and only say that these
3670 %% declarations are necessary for the flexible syntax you may use, and
3671 %% for realising record inheritances. Remember that a record may be written as
3672 %% \begin{vvtm}
3673 %% \begin{ccode}
3674 %% Date { year = 1984, month = 1, day = 1 }
3675 %% \end{ccode}
3676 %% \end{vvtm}
3677 %% or
3678 %% \begin{vvtm}
3679 %% \begin{ccode}
3680 %% Date { month = 1, day = 1, year = 1984 }
3681 %% \end{ccode}
3682 %% \end{vvtm}
3683 %% interchangeably; you may also omit uninteresting slots, as
3684 %% \begin{vvtm}
3685 %% \begin{ccode}
3686 %% Date { year = 1984 }
3687 %% \end{ccode}
3688 %% \end{vvtm}
3689 %% where \verb|month| and \verb|date| disappear;
3690 %% and \verb|set|, \verb|set-year| etc. will be declared for every record
3691 %% that inherits \verb|Date|.
3692
3693 %% Finally, look at axioms.
3694 %% \begin{vvtm}
3695 %% \begin{ccode}
3696 %% DATE> show axioms
3697 %% - Equations
3698 %% eq day(RID-5:RecordDate { (VAR-SLOTDAY-4:Slotday = VAL-7:Nat ,
3699 %% ATTRS-6:Attributes) }) = VAL-7:Nat
3700 %% eq set-day(RID-5:RecordDate { (VAR-SLOTDAY-4:Slotday = VAL-7:Nat ,
3701 %% ATTRS-6:Attributes) }, NEW-VAL-8)
3702 %% = RID-5:RecordDate { (VAR-SLOTDAY-4:Slotday = NEW-VAL-8 ,
3703 %% ATTRS-6:Attributes) }
3704 %% \end{ccode}
3705 %% \end{vvtm}
3706 %% Again we omitted the rest of the (long) output. The above two equations
3707 %% define \verb|day| and \verb|set-day|. Their simplified forms are
3708 %% \begin{vvtm}
3709 %% \begin{ccode}
3710 %% eq day (Date { day = D, month = M, year = Y }) = D .
3711 %% eq set-day (Date { day = D, month = M, year = Y }, D') =
3712 %% Date { day = D', month = M, year = Y } .
3713 %% \end{ccode}
3714 %% \end{vvtm}
3715 %% which look exactly like the access functions to \verb|day|.
3716
3717 \section{Some Typical Modules}\label{sec:p2-typical-modules}
3718
3719 \subsection{Unique Models}\label{sec:p2-unique-model}
3720
3721 When you write a specification of natural numbers (Section
3722 \ref{sec:p2-print-whole-module}), your probable intention is
3723 to specify ``the'' natural numbers. For example, you do not
3724 want a singleton $\{ 0 \}$ to constitute a model of
3725 your specification (the elements are too few), nor a colossal
3726 sum $\mathcal{N} + \mathcal{N} + \ldots + \mathcal{N}$ (too many).
3727 You want exactly the set $\{ 0, 1, 2, \ldots \}$ to be the
3728 model of your specification.
3729
3730 In the terminology of \cafeobj, such a module should be interpreted to have
3731 tight denotation: it has exactly one model --- the usual term
3732 model. A module introduced with ``\verb|module!|'' is always so interpreted.
3733 So perhaps it is more to your taste if we have written
3734 \begin{vvtm}
3735 \begin{ccode}
3736 module! SIMPLE-NAT {
3737 ...
3738 }
3739 \end{ccode}
3740 \end{vvtm}
3741 As another example of tight denotation, consider indeterminate
3742 choice (cf. Section \ref{exs:choice}) of natural numbers\footnote{
3743 The current implementation imports a
3744 builtin sort of the same name, so you need a lot of qualifications
3745 for this example to work correctly.
3746
3747 }.
3748 \label{exs:choice-nat}
3749 \begin{vvtm}
3750 \begin{ccode}
3751 module! CHOICE-NAT {
3752 extending (SIMPLE-NAT)
3753 op _|_ : Nat Nat -> Nat
3754 vars N N' : Nat
3755 trans N | N' => N .
3756 trans N | N' => N' .
3757 }
3758 \end{ccode}
3759 \end{vvtm}
3760 In the model you have in mind,
3761 \begin{itemize}
3762 \item[] \verb+0 | s(0)+ may transit to \verb|0|
3763 \item[] \verb+0 | s(0)+ may transit to \verb|s(0)|
3764 \end{itemize}
3765 but
3766 \begin{itemize}
3767 \item[] \verb+0 | s(0)+ should not transit to \verb|s(s(0))|
3768 \end{itemize}
3769 \verb|module!| ensures that the last transition is not in the model.
3770 To cope with transition relations, the model must have a 2-categorical
3771 structure\footnote{
3772 In fact, since \cafeobj~does not distinguish transitions between the
3773 same two elements, a more restricted structure --- essentially,
3774 partial orders and monotonic functions --- serves as well.
3775 }; otherwise, everything is a straightforward extension to usual
3776 algebraic semantics.
3777
3778 \subsection{Inspecting Transition Axioms}\label{sec:p2-trans-beget-axs}
3779
3780 When you declare a transition, a couple of axioms are automatically
3781 generated (Section \ref{sec:p2-transition-pred}). Let us use
3782 \verb|CHOICE-NAT| for illustration.
3783 \begin{vvtm}
3784 \begin{ccode}
3785 CafeOBJ> module! CHOICE-NAT {
3786 extending (SIMPLE-NAT)
3787 op _|_ : Nat Nat -> Nat
3788 vars N N' : Nat
3789 trans N | N' => N .
3790 trans N | N' => N' .
3791 }
3792
3793 -- defining module! CHOICE-NAT..._._.* done.
3794 CafeOBJ> select CHOICE-NAT
3795
3796 CHOICE-NAT> show axioms
3797 - Equations
3798 ceq cv1:Nat | cv2:Nat ==> cv3:Nat | cv4:Nat = true
3799 if cv1:Nat ==> cv3:Nat and cv2:Nat ==> cv4:Nat
3800 ceq s(cv1:Nat) ==> s(cv2:Nat) = true if cv1:Nat ==> cv2:Nat
3801 ceq cv1:Nat + cv2:Nat ==> cv3:Nat + cv4:Nat = true
3802 if cv1:Nat ==> cv3:Nat and cv2:Nat ==> cv4:Nat
3803 eq N | N' ==> N' = true
3804 eq N | N' ==> N = true
3805 - Rules
3806 trans N | N' => N
3807 trans N | N' => N'
3808
3809 CHOICE-NAT> show subs
3810 extending(SIMPLE-NAT)
3811 protecting(RWL)
3812
3813 CHOICE-NAT>
3814 \end{ccode}
3815 \end{vvtm}
3816 As shown above, congruence axioms are added, and a
3817 module called \verb|RWL| is imported automatically. \verb|RWL| is
3818 the module that declares ``\verb|_==>_|''.
3819
3820 \begin{warning}
3821 By the way, where is the reflexivity axiom? Well, it is hidden
3822 in the module \verb|RWL|. The current implementation provides
3823 a ``universal'' reflexivity axiom, and each module does not
3824 acquire a new axiom. But you should think as if it did.
3825 \end{warning}
3826
3827 \subsection{Non-isomorphic Models}\label{sec:p2-non-iso-model}
3828
3829 There are cases when your favourite models are not unique. Examples
3830 abound in defining algebraic structures. Monoids may be defined as
3831 \label{exs:monoid}
3832 \begin{vvtm}
3833 \begin{ccode}
3834 module* MONOID {
3835 [ M ]
3836 op e : -> M
3837 op _*_ : M M -> M
3838 vars X Y Z : M
3839 eq (X * Y) * Z = X * (Y * Z) .
3840 eq e * X = X .
3841 eq X * e = X .
3842 }
3843 \end{ccode}
3844 \end{vvtm}
3845 When you write down the above module, what is intended is not
3846 (only) a trivial monoid, the singleton $\{ e \}$. But that is
3847 exactly the tight denotation of this module, which you would get
3848 if it were declared with ``\verb|module!|''. In this case, a module
3849 should accommodate any model so long as it has an associative
3850 binary operator with an --- i.e., the --- identity element.
3851 A module introduced with ``\verb|module*|'' is always interpreted as such.
3852
3853 Another example uses behavioural operators.\label{exs:counter}
3854 \begin{vvtm}
3855 \begin{ccode}
3856 module* COUNTER {
3857 protecting (SIMPLE-NAT)
3858 *[ Counter ]*
3859 bop read : Counter -> Nat
3860 bop add : Nat Counter -> Counter
3861 eq read(add(N:Nat, C:Counter)) = N + read(C) .
3862 }
3863 \end{ccode}
3864 \end{vvtm}
3865 Your intention is to define a counter that is incremented by
3866 the operator \verb|add|. Tight denotation would lead to the model
3867 where, e.g., for a fixed counter \verb|c|,
3868 \begin{vvtm}
3869 \begin{ccode}
3870 add(s(0), add(s(s(0)), c))
3871 add(s(s(0)), add(s(0), c))
3872 \end{ccode}
3873 \end{vvtm}
3874 are different elements. Intuitively, this model says that
3875 adding 1, then 2, is different from adding 2, then 1. When your
3876 sole concern is the return values of \verb|read|, this model
3877 works fine (adding 2, then 1 increments the counter by 3;
3878 so does adding 1, then 2). But there is no reason to
3879 preclude a model where the above two terms denote the same
3880 element (the order of addition is immaterial). Hence this
3881 module was introduced with ``\verb|module*|'', as above.
3882
3883 \subsection{Inspecting Hidden Sorts}\label{sec:p2-hsort-beget-axs}
3884
3885 Two terms of a hidden sort is equal, or {\em behaviourally equivalent},
3886 \index{behaviourally equivalent}, iff they behave the same under
3887 any {\em behavioural context} (cf. \cite{hsa}),\index{behavioural context}
3888 \index{context (behavioural)} or {\em observation}\index{observation}.
3889 An observation is a term of a visible sort, which contains
3890 exactly one (occurrence of one) variable of the hidden sort.
3891 (So an observation is a special derived operator --- see
3892 Section \ref{sec:p2-pre-view}.)
3893 In the example of \verb|COUNTER|, the observations for \verb|Counter|
3894 is of the form
3895 \begin{vvtm}
3896 \begin{ccode}
3897 read(X), read(add(m,X)), read(add(m,add(m',X))), ...
3898 \end{ccode}
3899 \end{vvtm}
3900 where \verb|X| is a variable of sort \verb|Counter|, and
3901 \verb|m|, \verb|m'| are arbitrary terms of sort \verb|Nat|.
3902 For this special case, therefore, the above definition means that
3903 two terms \verb|t|, \verb|t'| of sort \verb|Counter| are
3904 behaviourally equal iff the equations
3905 \begin{vvtm}
3906 \begin{ccode}
3907 eq read(t) = read(t') .
3908 eq read(add(m,t)) = read(add(m,t')) .
3909 eq read(add(m,add(m',t))) = read(add(m,add(m',t'))) .
3910 ...
3911 \end{ccode}
3912 \end{vvtm}
3913 hold.
3914
3915 Unlike equality over visible sorts, equality over hidden sorts
3916 are unlikely to be determined by straightforward reduction; two terms
3917 may well turn out to be behaviourally equivalent, even if they have
3918 different irreducible forms. It is still possible, however, to
3919 use reduction to {\em help} prove behavioural equivalence.
3920
3921 As explained in Section \ref{sec:p2-beh-equiv-pred}, the system
3922 associates a binary predicate ``\verb|_=*=_|'' to each hidden sort,
3923 and declare an axiom. You may inspect the detail,
3924 as the following example shows.
3925 \begin{vvtm}
3926 \begin{ccode}
3927 CafeOBJ> module* COUNTER {
3928 protecting (SIMPLE-NAT)
3929 *[ Counter ]*
3930 bop read : Counter -> Nat
3931 bop add : Nat Counter -> Counter
3932 eq read(add(N:Nat, C:Counter)) = N + read(C) .
3933 }
3934
3935 -- defining module* COUNTER...._.*
3936 * system already proved =*= is a congruence of COUNTER done.
3937 CafeOBJ> select COUNTER
3938
3939 COUNTER> show axioms
3940 - Equations
3941 eq read(add(N:Nat,C:Counter)) = N:Nat + read(C:Counter)
3942 ceq hs1:Counter =*= hs2:Counter = true if read(hs1:Counter) ==
3943 read(hs2:Counter)
3944
3945 COUNTER>
3946 \end{ccode}
3947 \end{vvtm}
3948 In processing \verb|COUNTER| the system added an equation
3949 after checking congruence.
3950
3951 \chapter{Evaluating Terms}\label{sec:p2-term-evaluation}
3952
3953 \cafeobj~has an operational semantics based on term rewriting system
3954 (TRS)s. This chapter introduces the very basics of TRSs and explains
3955 when a \cafeobj~code constitutes a TRS.
3956
3957 \section{Term Rewriting System}\label{sec:p2-trs}
3958
3959 \subsection{What is a TRS}\label{sec:p2-trs-basic}
3960
3961 This section is an intuitive introduction to the computation model of
3962 TRS's\index{TRS}\index{term rewriting system}. First, a preliminary.
3963
3964 \begin{itemize}
3965 \item[] {\em Substitution}\index{substitution}.
3966 A substitution is a set $S$ of pairs $(v,t)$ of a variable $v$ and a
3967 (ground) term $t$, such that no two pairs contain the same variable.
3968 If $(v,t)$ is in $S$, $t$ is said to be the {\em value} of $v$ under
3969 $A$.
3970 \item[] {\em Instantiation}\index{instantiation of a term}.
3971 Given a substitution $S$ and a term $t$, replacing each (occurrence of
3972 each) variable in $t$ with its value under $S$ is called the
3973 instantiation of $t$ by $S$.
3974 \item[] {\em Matching}\index{matching}. Given a {\em ground} term
3975 \index{ground term} $t$ and a
3976 term $p$, if there is a substitution $S$ such that the instantiation
3977 of $p$ by $S$ equals $t$, $t$ is said to be matchable to $p$.
3978 To compute such a substitution is called matching.
3979 \end{itemize}
3980 Given a set $R$ of rewrite rules, computation with $R$ as TRS
3981 proceeds as follows: given a {\em ground} term $t$,
3982 \begin{itemize}
3983 \item[1.] (pattern matching) Find a rewrite rule in $R$ such that a
3984 subterm $s$ of $t$
3985 is matchable to its lefthand side.
3986 \item[2.] (rewrite) If such a rewrite rule is found, replace
3987 $s$ in $t$ with
3988 the instance of its righthand by the matching substitution, and
3989 go back to 1. with the resulting new term.
3990 \item[3.] If no such rewrite rule is found, terminate.
3991 \end{itemize}
3992 This procedure is called {\em evaluation}\index{evaluation},
3993 or {\em reduction}\index{reduction}, of $t$. If the procedure terminates,
3994 the resultant term is said to be {\em irreducible}\index{irreducible (term)},
3995 or {\em normal}\index{normal (term)}.
3996 Note well that this procedure is for ground terms only.
3997
3998 If a rewrite rule is conditional, in addition to matchability,
3999 you also have to check the condition. To check the condition,
4000 its instance by the matching substitution is evaluated. The rule is
4001 applicable only when the condition evaluates to (a term denoting)
4002 true. A TRS with conditional rewrite rules is called conditional
4003 TRS.
4004
4005 Another refinement of the above procedure is to match terms
4006 not only syntactically, but modulo an equational theory $E$.
4007 Such a mathching procedure is called $E$-matching\index{E-matching}.
4008 A typical equational theory is associativity. Modulo associativity
4009 on an operator ``\verb|_*_|'', for example,
4010 ``\verb|a * (b * c)|'' matches ``\verb|(X * b) * Y|'',
4011 where \verb|X|, \verb|Y| are variables, since the former term is
4012 equal to ``\verb|(a * b) * c|''. \cafeobj~does support this kind of
4013 matching (cf. Section \ref{sec:p2-equational-theory}).
4014
4015 There may be cases where more than one rules match with
4016 more than one subterms --- so the above procedure
4017 is, in fact, hardly a procedure at all. Which one to choose, and when, is a
4018 significant issue. \cafeobj~allows you to control the selection, operator
4019 by operator. See Section \ref{sec:p2-rew-strategy}.
4020
4021 \subsection{How a Module Defines a TRS}\label{sec:p2-executable-module}
4022
4023 The axioms of \cafeobj~are equations and transitions, which define
4024 equivalence and transition relations. In operational semantics,
4025 these axioms are regarded, uniformly, as directed rewrite rules,
4026 as follows. If we denote a rewrite rule by \verb|t -> t'|,
4027 for relevant terms \verb|t|, \verb|t'|, and \verb|b|,
4028 \begin{itemize}
4029 \item[] \verb|eq t = t' .| is a rewrite rule \verb|t -> t'|.
4030 \item[] \verb|ceq t = t' if b .| is a rewrite rule \verb|t -> t'|
4031 with the condition \verb|b|.
4032 \item[] \verb|trans t => t' .| is a rewrite rule \verb|t -> t'|.
4033 \item[] \verb|ctrans t => t' if b .| is a rewrite rule \verb|t -> t'|
4034 with the condition \verb|b|.
4035 \end{itemize}
4036 Behavioural axioms are translated similarly.
4037 Then the TRS defined by a module is the set of axioms regarded
4038 as rewrite rules as above. Note that the axioms of the imported
4039 modules are included in this definition.
4040
4041 For example, \verb|SIMPLE-NAT| (Section \ref{exs:simple-nat})
4042 defines a TRS that contains two rewrite rules
4043 \begin{vvtm}
4044 \begin{ccode}
4045 0 + N -> N
4046 s(N) + N' -> s(N + N')
4047 \end{ccode}
4048 \end{vvtm}
4049 where \verb|N|, \verb|N'| are variables of sort \verb|Nat|.
4050 Since \verb|BOOL| is implicitly imported, its axioms (as rewrite rules)
4051 are also in this TRS. \verb|show| command may be used to list all
4052 the rules (see examples in Section \ref{sec:p2-eval-exs-1}).
4053
4054 Two kinds of axioms are excluded from TRS's, solely
4055 by syntactic criteria. For an axiom to be a rewrite rule,
4056 \begin{itemize}
4057 \item[(1)] Every variable that occurs in the righthand side or in the
4058 condition must occur in the lefthand side also. Otherwise the terms
4059 under computation may become non-ground.
4060 \item[(2)] The lefthand side must not be a single variable. Otherwise
4061 every term (of a relevant sort) is matchable, so the computation never
4062 stops.
4063 \end{itemize}
4064 For order-sorted TRS, there might be other constraints, such as
4065 sort-decreasing property. But \cafeobj~does not impose such a constraint.
4066
4067 There are two important properties of TRSs.
4068 If computation always terminates, the TRS is said to be {\em terminating}.
4069 \index{terminating} If, whenever a term is rewritten to two different
4070 terms, they can be further rewritten to a same term, the TRS is said to
4071 be {\em confluent}\index{confluent}. If terminating and confluent, it is
4072 {\em canonical}.\index{canonical (TRS)}
4073
4074 In general, the declarative meaning of
4075 an axiom set differs from the operational one (i.e. TRS). But
4076 there {\em is} a coincidence if we restrict ourselves to tight
4077 denotation and equations.
4078 If the set, regarded as TRS, is canonical, i.e. terminating and confluent,
4079 \begin{quote}
4080 Two (ground) terms are equal iff they are reducible to a common term.
4081 \end{quote}
4082 For this reason, it is important to distinguish a subset of
4083 a TRS derived only from equations (cf. \verb|reduce| command in
4084 Section \ref{sec:p2-evaluation-command}). It also follows that
4085 for loose denotation, reduction per se does not suggest anything
4086 of much import. (For theorem proving, it {\em is} useful --- see
4087 Section \ref{sec:p2-theorem-proving-exs}).
4088
4089 \section{Do the Evaluation}
4090
4091 \subsection{Evaluation Commands}\label{sec:p2-evaluation-command}
4092
4093 There are three commands for evaluating terms.
4094
4095 \begin{bsyntax} \texttt{reduce} command \index{\texttt{reduce}} \Hline
4096 \texttt{reduce} $[$\texttt{in} \textit{module\_expression} ``\texttt{:}''$]$ \textit{term} ``\texttt{.}''
4097 \end{bsyntax}
4098
4099 \begin{bsyntax} \texttt{breduce} command \index{\texttt{breduce}} \Hline
4100 \texttt{breduce} $[$\texttt{in} \textit{module\_expression} ``\texttt{:}'' $]$ \textit{term} ``\texttt{.}''
4101 \end{bsyntax}
4102
4103 \begin{bsyntax} \texttt{execute} command \index{\texttt{execute}} \Hline
4104 \texttt{execute} $[$\texttt{in} \textit{module\_expression} ``\texttt{:}''$]$ \textit{term} ``\texttt{.}''
4105 \end{bsyntax}
4106
4107 As is ever the case, the period after a blank is a must.
4108 \verb|reduce| can be abbreviated to \verb|red|, \verb|breduce| to
4109 \verb|bred|, and
4110 \verb|execute| to \verb|exec|. Module expressions are introduced in
4111 Section \ref{sec:p2-module-expression}, and we only consider
4112 module names here. These commands evaluate the term with a TRS defined
4113 by the given module; or by the current module
4114 (Section \ref{sec:p2-current-module}),\index{current module}
4115 if the module expression is omitted.
4116
4117 These three commands differ on (1) which axioms constitute the TRS
4118 and (2) applicability of some rewrite rules.
4119 \begin{itemize}
4120 \item[(1)]
4121 \begin{itemize}
4122 \item For \verb|reduce| or \verb|breduce|, only --- possibly conditional,
4123 possibly behavioural --- equations are taken to constitute the TRS.
4124 Transitions are excluded. This ensures that under certain conditions
4125 (Section \ref{sec:p2-executable-module}) \verb|reduce| gives a
4126 decision procedure for equational theory.
4127 \item For \verb|execute|, all the axioms constitute the TRS.
4128 \end{itemize}
4129 \item[(2)]
4130 \begin{itemize}
4131 \item For \verb|reduce| or \verb|execute|, a rewrite rule that
4132 come from a behavioural axiom is applicable only when the
4133 redex in enclosed in an observation. This ensures the
4134 soundness of evaluation.
4135 \item For \verb|breduce|, there is no such restriction.
4136 \end{itemize}
4137 \end{itemize}
4138 The point (1) comes from the fact that \verb|reduce| (\verb|breduce|) is
4139 to get a term equal (behaviourally equivalent)
4140 to the input, while \verb|execute| is to get a term
4141 that the input may eventually transit to.
4142 The point (2) comes from the fact that \verb|breduce| is to get a term
4143 behaviourally equivalent to the input, while \verb|reduce| is to get a
4144 term equal to the input. These points are illustated in
4145 Sections \ref{sec:p2-eval-exs-2} and \ref{sec:p2-eval-exs-3}.
4146
4147 %On the point (2), consider the following declarations.
4148 %\begin{vvtm}
4149 %\begin{ccode}
4150 % *[ S ]*
4151 % [ T ]
4152 % ops a b : -> S
4153 % bop f : S -> T
4154 % beq [ a-sim-b ] a = b .
4155 %\end{ccode}
4156 %\end{vvtm}
4157 %The axiom \verb|a-sim-b| asserts that \verb|a| is behaviourally
4158 %equivalent to \verb|b|, but does not say that they are equal.
4159 %Therefore such an axiom should not be used to conclude
4160 %that \verb|a| equals \verb|b|. On the other hand, since
4161 %\verb|a| and \verb|b| are behaviourally equivalent, every
4162 %observation of the two terms is equal. A forteriori
4163 %\verb|f(a)| equals \verb|f(b)|. In reasoning the equality,
4164 %the axiom \verb|a-sim-b| is safely used. This is the gist of
4165 %the restriction stated in (2).
4166
4167 During evaluation, it is sometimes desirable to see the rewrite sequences,
4168 not just the results. For example, to enhance efficiency, you may want to
4169 know where redundant computations of subterms occur. For this purpose,
4170 \cafeobj~provides trace switches, which can be set by\index{\texttt{set}}
4171 \index{\texttt{trace}}\index{\texttt{trace whole}}\index{\texttt{whole}}
4172 \index{trace (of evaluation)}
4173 \begin{vvtm}
4174 \begin{ccode}
4175 CafeOBJ> set trace whole on
4176 \end{ccode}
4177 \end{vvtm}
4178 by which the resultant term of each rewrite step is printed, or
4179 \begin{vvtm}
4180 \begin{ccode}
4181 CafeOBJ> set trace on
4182 \end{ccode}
4183 \end{vvtm}
4184 by which the rule, substitution, and replacement are printed. The
4185 effects of these switches shall be illustrated by examples
4186 below.
4187
4188 \subsection{Replacing Equals by Equals}\label{sec:p2-eval-exs-1}
4189
4190 Recall \verb|SIMPLE-NAT| (Section \ref{exs:simple-nat}) again,
4191 make it current, and do some evaluations.
4192 \begin{vvtm}
4193 \begin{ccode}
4194 SIMPLE-NAT> reduce 0 .
4195
4196 -- reduce in SIMPLE-NAT : 0
4197 0 : Zero
4198 (0.000 sec for parse, 0 rewrites(0.000 sec), 0 matches)
4199 SIMPLE-NAT> reduce 0 + s(0) .
4200
4201 -- reduce in SIMPLE-NAT : 0 + s(0)
4202 s(0) : NzNat
4203 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
4204
4205 SIMPLE-NAT> reduce s(0) + 0 .
4206
4207 -- reduce in SIMPLE-NAT : s(0) + 0
4208 s(0) : NzNat
4209 (0.010 sec for parse, 2 rewrites(0.000 sec), 3 matches)
4210 SIMPLE-NAT>
4211 \end{ccode}
4212 \end{vvtm}
4213 The output from the system has three parts, showing
4214 the system's understanding of what to do (the line starting with
4215 ``\verb|--|''), the result of evaluation (the second line), and
4216 what looks like statistics (in parentheses). The result is of the form
4217 \begin{vvtm}
4218 \begin{ccode}
4219 term : sort
4220 \end{ccode}
4221 \end{vvtm}
4222 where \verb|sort| is the least sort (Section \ref{sec:p2-wff-terms})
4223 of \verb|term|.
4224
4225 You may print the rules of \verb|SIMPLE-NAT| as follows.\index{\texttt{show}}
4226 \index{\texttt{rules}}
4227 \begin{vvtm}
4228 \begin{ccode}
4229 SIMPLE-NAT> show rules
4230 -- rewrite rules in module : SIMPLE-NAT
4231 1 : eq 0 + N = N
4232 2 : eq s(N) + N' = s(N + N')
4233
4234 SIMPLE-NAT>
4235 \end{ccode}
4236 \end{vvtm}
4237 Note that all the axioms of the implicitly imported \verb|BOOL|
4238 are also in the TRS, but were not printed above. It is possible to
4239 print all the rules, including those declared in the imported modules,
4240 by an extra argument to \verb|show|.
4241 \begin{vvtm}
4242 \begin{ccode}
4243 SIMPLE-NAT> show all rules
4244 -- rewrite rules in module : SIMPLE-NAT
4245 1 : eq 0 + N = N
4246 2 : eq s(N) + N' = s(N + N')
4247 3 : eq not true = false
4248 4 : eq not false = true
4249 5 : eq false and A:Bool = false
4250 6 : eq true or A:Bool = true
4251 7 : eq true and-also A:Bool = A:Bool
4252 8 : eq A:Bool and-also true = A:Bool
4253 9 : eq false and-also A:Bool = false
4254 10 : eq A:Bool and-also false = false
4255 11 : eq true or-else A:Bool = true
4256 12 : eq A:Bool or-else true = true
4257 13 : eq false or-else A:Bool = A:Bool
4258 14 : eq A:Bool or-else false = A:Bool
4259 15 : eq A:Bool implies B:Bool = not A:Bool or B:Bool
4260 16 : eq true xor true = false
4261 17 : eq CXU : Id:SortId = #!! (coerce-to-bool
4262 (test-term-sort-membership cxu id))
4263 18 : eq if true then CXU else CYU fi = CXU
4264 19 : eq if false then CXU else CYU fi = CYU
4265 20 : eq CXU == CYU = #!! (coerce-to-bool (term-equational-equal cxu cyu))
4266 21 : eq HXU =b= HYU = #!! (coerce-to-bool (term-equational-equal hxu hyu))
4267 22 : eq CXU =/= CYU = #!! (coerce-to-bool
4268 (not (term-equational-equal cxu cyu)))
4269
4270 SIMPLE-NAT>
4271 \end{ccode}
4272 \end{vvtm}
4273
4274 \begin{warning}
4275 The righthand sides of the rules \verb|17| and so on contain
4276 a strange symbol \verb|#!!|, and the terms that follow
4277 suspiciously resemble Lisp's S-expressions. These are examples of
4278 system-dependent definitions of operators. If you are not
4279 confident enough about implementation issues, do not touch
4280 these definitions.
4281 \end{warning}
4282
4283 Since the axioms of \verb|SIMPLE-NAT| are all equations, the results of
4284 \verb|reduce| and \verb|execute| should be the same. Indeed, you have
4285 \begin{vvtm}
4286 \begin{ccode}
4287 SIMPLE-NAT> execute 0 + s(0) .
4288
4289 -- execute in SIMPLE-NAT : 0 + s(0)
4290 s(0) : NzNat
4291 (0.010 sec for parse, 1 rewrites(0.000 sec), 1 matches)
4292 SIMPLE-NAT> execute s(0) + 0 .
4293
4294 -- execute in SIMPLE-NAT : s(0) + 0
4295 s(0) : NzNat
4296 (0.000 sec for parse, 2 rewrites(0.000 sec), 3 matches)
4297 SIMPLE-NAT>
4298 \end{ccode}
4299 \end{vvtm}
4300
4301 The statistics may be suppressed by a switch.\index{\texttt{set}}
4302 \index{\texttt{stats}}
4303 \begin{vvtm}
4304 \begin{ccode}
4305 SIMPLE-NAT> set stats off
4306
4307 SIMPLE-NAT> reduce s(0) + 0 .
4308
4309 -- reduce in SIMPLE-NAT : s(0) + 0
4310 s(0) : NzNat
4311 SIMPLE-NAT>
4312 \end{ccode}
4313 \end{vvtm}
4314
4315 \begin{warning}
4316 \verb|stats| is not an abbreviation.
4317 \end{warning}
4318
4319 \subsection{Equations and Transitions}\label{sec:p2-eval-exs-2}
4320
4321 To see the difference between \verb|execute| and \verb|reduce|,
4322 we switch to \verb|CHOICE-NAT| (Section \ref{exs:choice-nat}).
4323 \begin{vvtm}
4324 \begin{ccode}
4325 SIMPLE-NAT> select CHOICE-NAT
4326
4327 CHOICE-NAT> set stats on
4328
4329 CHOICE-NAT> reduce s(0) | (s(s(0)) + s(s(s(0)))) .
4330
4331 -- reduce in CHOICE-NAT : s(0) | (s(s(0)) + s(s(s(0))))
4332 s(0) | s(s(s(s(s(0))))) : Nat
4333 (0.010 sec for parse, 3 rewrites(0.000 sec), 5 matches)
4334
4335 CHOICE-NAT> execute s(0) | (s(s(0)) + s(s(s(0)))) .
4336
4337 -- execute in CHOICE-NAT : s(0) | (s(s(0)) + s(s(s(0))))
4338 s(0) : NzNat
4339 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
4340
4341 CHOICE-NAT> show all rules
4342 -- rewrite rules in module : CHOICE-NAT
4343 1 : ceq cv1:Nat | cv2:Nat ==> cv3:Nat | cv4:Nat = true if cv1:Nat ==>
4344 cv3:Nat and cv2:Nat ==> cv4:Nat
4345 2 : ceq s(cv1:Nat) ==> s(cv2:Nat) = true if cv1:Nat ==> cv2:Nat
4346 3 : ceq cv1:Nat + cv2:Nat ==> cv3:Nat + cv4:Nat = true if cv1:Nat ==>
4347 cv3:Nat and cv2:Nat ==> cv4:Nat
4348 4 : eq N | N' ==> N' = true
4349 5 : eq N | N' ==> N = true
4350 6 : trans N | N' => N
4351 7 : trans N | N' => N'
4352 8 : eq 0 + N = N
4353 9 : eq s(N) + N' = s(N + N')
4354 10 : ...
4355 \end{ccode}
4356 \end{vvtm}
4357 We omitted the rest of the listing, which contains axioms in
4358 \verb|BOOL|. \verb|reduce| command ignores transition declarations
4359 \verb|6| and \verb|7|, so the reduction stopped after rewriting
4360 the second argument of \verb+_|_+. \verb|execute| applied the
4361 rule \verb|6| immediately, and just one rewrite (see the statistics)
4362 led to the result.
4363
4364 \begin{warning}
4365 Technically, the TRS faithful to the underlying model
4366 should be a TRS, derived entirely from transitions, over equivalence
4367 classes of terms, determined by equations. The current system
4368 does not distinguish transitions and equations during evaluation,
4369 and acts faithfully only when certain conditions are satisfied.
4370 For detail, see \cite{rew-logic}.
4371 \end{warning}
4372
4373 \subsection{Using Behavioural Equations}\label{sec:p2-eval-exs-3}
4374
4375 As a matter of course, behavioural equations can be
4376 used to prove behavioural equivalence. With judicious use, they can also be
4377 used to prove equality. Consider the following example.
4378 \label{exs:nat-stream}
4379 \begin{vvtm}
4380 \begin{ccode}
4381 module* NAT-STREAM {
4382 protecting (SIMPLE-NAT)
4383 *[ Stream ]*
4384 bop __ : Nat Stream -> Stream
4385 bop hd : Stream -> Nat
4386 bop tl : Stream -> Stream
4387 op zeros : -> Stream
4388 var N : Nat
4389 var S : Stream
4390 eq hd(N S) = N .
4391 beq tl(N S) = S .
4392 eq hd(zeros) = 0 .
4393 beq tl(zeros) = zeros .
4394 }
4395 \end{ccode}
4396 \end{vvtm}
4397 A behavioural equation asserts that \verb|tl(n s)| is behaviourally
4398 equivalent to \verb|s| for any \verb|n| and \verb|s|, but does not
4399 assert that they are equal. Therefore, on the one hand,
4400 it is incorrect to reason
4401 that \verb|tl(s(0) zeros)| equals \verb|zeros| from this axiom.
4402 On the other hand, it is correct to reason
4403 that \verb|hd(tl(s(0) zeros))| is equal to \verb|hd(zeros)| and hence
4404 to \verb|0|, since \verb|hd|, being an observation on \verb|Stream|,
4405 gives the same answer if applied to behaviourally equivalent terms.
4406
4407 In processing \verb|reduce|, the system tries to apply behavioural
4408 equations (as rewrite rules) only when the redex is under an
4409 observation, as shown below.
4410 \begin{vvtm}
4411 \begin{ccode}
4412 CafeOBJ> select NAT-STREAM
4413
4414 NAT-STREAM> reduce hd(tl(s(0) zeros)) .
4415
4416 -- reduce in NAT-STREAM : hd(tl(s(0) zeros))
4417 0 : Zero
4418 (0.010 sec for parse, 2 rewrites(0.000 sec), 3 matches)
4419 NAT-STREAM> reduce tl(s(0) zeros) .
4420
4421 -- reduce in NAT-STREAM: tl(s(0) zeros)
4422 tl(s(0) zeros) : Stream
4423 (0.000 sec for parse, 0 rewrites(0.000 sec), 0 matches)
4424 NAT-STREAM>
4425 \end{ccode}
4426 \end{vvtm}
4427 Compare the above result to the case when \verb|breduce| is
4428 invoked instead. Since this command is to get an behaviourally
4429 equivalent term, the system applies behavioural equations
4430 indiscriminately, as
4431 \begin{vvtm}
4432 \begin{ccode}
4433 NAT-STREAM> breduce tl(s(0) zeros) .
4434
4435 -- behavioural reduce in NAT-STREAM : tl(s(0) zeros)
4436 zeros : Stream
4437 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
4438 NAT-STREAM>
4439 \end{ccode}
4440 \end{vvtm}
4441
4442 \subsection{Evaluation Traces}\label{sec:p2-eval-trace}
4443
4444 We continue the session of the previous section.
4445 The statistics contained the number of rewrites and match attempts.
4446 Reduction of ``\verb|0 + s(0)|'' and ``\verb|s(0) + 0|'' in Section
4447 \ref{sec:p2-eval-exs-1} amounts to computing $0 + 1$ and $1 + 0$
4448 respectively, and the statistics showed that the efforts of computation
4449 were greater with $1 + 0$. You probably know why. To confirm your reasoning,
4450 try trace switches.\index{\texttt{trace whole}}\index{trace (of evaluation)}
4451 \begin{vvtm}
4452 \begin{ccode}
4453 CHOICE-NAT> select SIMPLE-NAT
4454
4455 SIMPLE-NAT> set trace whole on
4456
4457 SIMPLE-NAT> reduce 0 + s(0) .
4458
4459 -- reduce in SIMPLE-NAT : 0 + s(0)
4460 [1]: 0 + s(0)
4461 ---> s(0)
4462 s(0) : NzNat
4463 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
4464 SIMPLE-NAT> reduce s(0) + 0 .
4465
4466 -- reduce in SIMPLE-NAT : s(0) + 0
4467 [1]: s(0) + 0
4468 ---> s(0 + 0)
4469 [2]: s(0 + 0)
4470 ---> s(0)
4471 s(0) : NzNat
4472 (0.000 sec for parse, 2 rewrites(0.010 sec), 3 matches)
4473 SIMPLE-NAT>
4474 \end{ccode}
4475 \end{vvtm}
4476 The system printed the reduction traces. They show that the
4477 first reduction immediately cancelled a right identity zero,
4478 while the second reduction, which could not rely on the knowledge
4479 that zero is also a left identity, took two rewrites to
4480 get the result.
4481
4482 Sometimes more elaborate traces are desirable.\index{\texttt{trace}}
4483 \begin{vvtm}
4484 \begin{ccode}
4485 SIMPLE-NAT> set trace whole off
4486
4487 SIMPLE-NAT> set trace on
4488
4489 SIMPLE-NAT> reduce s(0) + 0 .
4490
4491 -- reduce in SIMPLE-NAT : s(0) + 0
4492 1>[1] rule: eq s(N:Nat) + N':Nat = s(N:Nat + N':Nat)
4493 { N':Nat |-> 0, N:Nat |-> 0 }
4494 1<[1] s(0) + 0 --> s(0 + 0)
4495 1>[2] rule: eq 0 + N:Nat = N:Nat
4496 { N:Nat |-> 0 }
4497 1<[2] 0 + 0 --> 0
4498 s(0) : NzNat
4499 (0.000 sec for parse, 2 rewrites(0.000 sec), 3 matches)
4500 SIMPLE-NAT>
4501 \end{ccode}
4502 \end{vvtm}
4503 This time, each rewrite step was explained in more detail.
4504 The three lines starting with ``\verb|1>|'' correspond to a
4505 single step. They show the rewrite rule (i.e. equation or
4506 transition) applied, the substitution used, and the replacement.
4507
4508 ``\verb|trace whole|'' switch traces the entire term, while
4509 replacements shown by \verb|trace| switch focus on the
4510 changed subterms. This difference was illustrated by the last
4511 step of this example, where ``\verb|trace whole|'' printed
4512 \begin{vvtm}
4513 \begin{ccode}
4514 [2] s(0 + 0) --> s(0)
4515 \end{ccode}
4516 \end{vvtm}
4517 while \verb|trace| printed
4518 \begin{vvtm}
4519 \begin{ccode}
4520 [2] 0 + 0 --> 0
4521 \end{ccode}
4522 \end{vvtm}
4523
4524 \subsection{Examples of Conditionals}\label{sec:p2-ex-cond}
4525
4526 To illustrate the r{\^ o}le of conditions, we first define an operator
4527 that computes greatest common divisors by filling more meat in the bony
4528 \verb|SIMPLE-NAT|.\label{exs:simple-nat+}\label{exs:gcd}
4529 \begin{vvtm}
4530 \begin{ccode}
4531 module SIMPLE-NAT+ {
4532 protecting (SIMPLE-NAT)
4533 op _-_ : Nat Nat -> Nat
4534 op _<_ : Nat Nat -> Bool
4535 -- ---------------------
4536 vars N M : Nat
4537 eq 0 - M = 0 .
4538 eq N - 0 = N .
4539 eq s(N) - s(M) = N - M .
4540 eq 0 < s(N) = true .
4541 eq N < 0 = false .
4542 eq s(N) < s(M) = N < M .
4543 }
4544
4545 module GCD {
4546 protecting (SIMPLE-NAT+)
4547 op gcd : Nat Nat -> Nat
4548 -- --------------------
4549 vars N M : Nat
4550 ceq gcd(N, M) = gcd(M, N) if N < M .
4551 eq gcd(N, 0) = N .
4552 ceq gcd(s(N), s(M)) = gcd(s(N) - s(M), s(M))
4553 if not (N < M) .
4554 }
4555 \end{ccode}
4556 \end{vvtm}
4557 The definitions of the pseudo-subtraction and the less than relation are
4558 more or less standard. \verb|gcd| is defined by conditional equations.
4559 Note that \verb|BOOL| is imported implicitly, so that all the operators
4560 in it, like ``\verb|not_|'' here, are available.
4561
4562 To see how these definitions work, continue the session in the previous
4563 section. Feed the modules to the system, and try as follows.
4564 \begin{vvtm}
4565 \begin{ccode}
4566 SIMPLE-NAT> set trace off
4567
4568 SIMPLE-NAT> select GCD
4569
4570 GCD> reduce gcd(s(s(s(s(s(s(0)))))), s(s(s(s(0))))) .
4571
4572 -- reduce in GCD : gcd(s(s(s(s(s(s(0)))))),s(s(s(s(0)))))
4573 s(s(0)) : NzNat
4574 (0.000 sec for parse, 44 rewrites(0.010 sec), 113 matches)
4575 GCD>
4576 \end{ccode}
4577 \end{vvtm}
4578 The reduction amounts to computing the greatest common divisor of
4579 $6$ and $4$. The statistics show that the system worked much harder
4580 than ever. You may get traces as before. Since the output is largish,
4581 we show traces of simpler computations (but this is still large).
4582 \begin{vvtm}
4583 \begin{ccode}
4584 GCD> set trace whole on
4585
4586 GCD> reduce gcd(s(s(s(s(0)))), s(s(0))) .
4587
4588 -- reduce in GCD : gcd(s(s(s(s(0)))),s(s(0)))
4589 [1(cond)]: s(s(s(s(0)))) < s(s(0))
4590 --> s(s(s(0))) < s(0)
4591 [2(cond)]: s(s(s(0))) < s(0)
4592 --> s(s(0)) < 0
4593 [3(cond)]: s(s(0)) < 0
4594 --> false
4595 [4(cond)]: not s(s(s(0))) < s(0)
4596 --> not s(s(0)) < 0
4597 [5(cond)]: not s(s(0)) < 0
4598 --> not false
4599 [6(cond)]: not false
4600 --> true
4601 [7]: gcd(s(s(s(s(0)))),s(s(0)))
4602 ---> gcd(s(s(s(s(0)))) - s(s(0)),s(s(0)))
4603 [8]: gcd(s(s(s(s(0)))) - s(s(0)),s(s(0)))
4604 ---> gcd(s(s(s(0))) - s(0),s(s(0)))
4605 [9]: gcd(s(s(s(0))) - s(0),s(s(0)))
4606 ---> gcd(s(s(0)) - 0,s(s(0)))
4607 [10]: gcd(s(s(0)) - 0,s(s(0)))
4608 ---> gcd(s(s(0)),s(s(0)))
4609 [11(cond)]: s(s(0)) < s(s(0))
4610 --> s(0) < s(0)
4611 [12(cond)]: s(0) < s(0)
4612 --> 0 < 0
4613 [13(cond)]: 0 < 0
4614 --> false
4615 [14(cond)]: not s(0) < s(0)
4616 --> not 0 < 0
4617 [15(cond)]: not 0 < 0
4618 --> not false
4619 [16(cond)]: not false
4620 --> true
4621 [17]: gcd(s(s(0)),s(s(0)))
4622 ---> gcd(s(s(0)) - s(s(0)),s(s(0)))
4623 [18]: gcd(s(s(0)) - s(s(0)),s(s(0)))
4624 ---> gcd(s(0) - s(0),s(s(0)))
4625 [19]: gcd(s(0) - s(0),s(s(0)))
4626 ---> gcd(0 - 0,s(s(0)))
4627 [20]: gcd(0 - 0,s(s(0)))
4628 ---> gcd(0,s(s(0)))
4629 [21(cond)]: 0 < s(s(0))
4630 --> true
4631 [22]: gcd(0,s(s(0)))
4632 ---> gcd(s(s(0)),0)
4633 [23(cond)]: s(s(0)) < 0
4634 --> false
4635 [24]: gcd(s(s(0)),0)
4636 ---> s(s(0))
4637 s(s(0)) : NzNat
4638 (0.000 sec for parse, 24 rewrites(0.070 sec), 60 matches)
4639 GCD>
4640 \end{ccode}
4641 \end{vvtm}
4642 The apparent difference from the previous traces is the presence of
4643 ``\verb|[(#)cond]|'' steps. As explained in Section
4644 \ref{sec:p2-trs-basic}, the system reduces conditions when it checks
4645 applicability of conditional rewrite rules. The above trace was the
4646 \index{conditional rewrite rule}
4647 result of following all of those recursive evaluations. For brevity
4648 we now resort to a very simple computation,
4649 and show only the initial part of the initial trace.
4650 \begin{vvtm}
4651 \begin{ccode}
4652 GCD> set trace whole off
4653
4654 GCD> set trace on
4655
4656 GCD> reduce gcd(s(s(0)),s(s(0))) .
4657
4658 -- reduce in GCD : gcd(s(s(0)),s(s(0)))
4659 1>[1] apply trial #1
4660 -- rule: ceq gcd(N:Nat,M:Nat) = gcd(M:Nat,N:Nat) if N:Nat < M:Nat
4661 { M:Nat |-> s(s(0)), N:Nat |-> s(s(0)) }
4662 2>[1] rule: eq s(N:Nat) < s(M:Nat) = N:Nat < M:Nat
4663 { M:Nat |-> s(0), N:Nat |-> s(0) }
4664 2<[1] s(s(0)) < s(s(0)) --> s(0) < s(0)
4665 2>[2] rule: eq s(N:Nat) < s(M:Nat) = N:Nat < M:Nat
4666 { M:Nat |-> 0, N:Nat |-> 0 }
4667 2<[2] s(0) < s(0) --> 0 < 0
4668 2>[3] rule: eq N:Nat < 0 = false
4669 { N:Nat |-> 0 }
4670 2<[3] 0 < 0 --> false
4671 1>[4] -- rewrite rule exhausted (#1)
4672 1>[4] apply trial #2
4673 -- rule: ceq gcd(s(N:Nat),s(M:Nat)) = gcd(s(N:Nat) - s(M:Nat),s(M:Nat)
4674 ) if not N:Nat < M:Nat
4675 { M:Nat |-> s(0), N:Nat |-> s(0) }
4676 2>[4] rule: eq s(N:Nat) < s(M:Nat) = N:Nat < M:Nat
4677 { M:Nat |-> 0, N:Nat |-> 0 }
4678 2<[4] s(0) < s(0) --> 0 < 0
4679 2>[5] rule: eq N:Nat < 0 = false
4680 { N:Nat |-> 0 }
4681 2<[5] 0 < 0 --> false
4682 2>[6] rule: eq not false = true
4683 {}
4684 2<[6] not false --> true
4685 1>[7] match success #2
4686 1<[7] gcd(s(s(0)),s(s(0))) --> gcd(s(s(0)) - s(s(0)),s(s(0)))
4687 \end{ccode}
4688 \end{vvtm}
4689 Note the appearances of ``\verb|2>|''. The number indicates the depth
4690 of recursion: evaluating conditions may lead to recursively
4691 evaluating conditions, and so on and on.
4692 Most of the output should be self-explanatory.
4693 The above trace shows two conditional rules were checked,
4694 first unsuccessfuly and second successfuly.
4695
4696 \section{Stepper}\label{sec:p2-stepper}
4697
4698 By taking traces, you may inspect in detail how reductions proceed,
4699 and modify axioms if they are not going as expected.
4700 The set of commands introduced in this section is for controlling
4701 reductions in more detail, and help you ``debug'' modules.
4702
4703 \subsection{Step Mode}\label{sec:p2-step-mode}
4704
4705 You may enter into/leave the step\index{step mode} mode by a switch.
4706 \index{\texttt{set}}\index{\texttt{step}}
4707 \begin{vvtm}
4708 \begin{ccode}
4709 CafeOBJ> set step on
4710 \end{ccode}
4711 \end{vvtm}
4712 In this mode, evaluation commands apply rewrite rules
4713 step by step. We first define multiplication.\label{exs:mult}
4714 \begin{vvtm}
4715 \begin{ccode}
4716 module MULT {
4717 protecting (SIMPLE-NAT)
4718 op _*_ : Nat Nat -> Nat
4719 vars N M : Nat
4720 eq 0 * N = 0 .
4721 eq s(N) * M = M + (N * M) .
4722 }
4723 \end{ccode}
4724 \end{vvtm}
4725 After feeding \verb|SIMPLE-NAT| and \verb|MULT|,
4726 you may get the following session.
4727 \begin{vvtm}
4728 \begin{ccode}
4729 CafeOBJ> select MULT
4730
4731 MULT> set step on
4732
4733 MULT> reduce s(s(0)) * s(s(s(0))) .
4734
4735 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4736 >> stepper term: s(s(0)) * s(s(s(0)))
4737 STEP[1]?
4738 \end{ccode}
4739 \end{vvtm}
4740 The new prompt indicates you are in the step mode.
4741 You may list the commands available in this mode
4742 by the help command ``\verb|:?|''.\index{\texttt{:?}}
4743 \begin{vvtm}
4744 \begin{ccode}
4745 STEP[1]? :?
4746 -- Stepper command help :
4747 ? print out this help
4748 n(ext) go one step
4749 g(o) <number> go <number> step
4750 c(ontinue) continue rewriting without stepping
4751 q(uit) leave stepper continuing rewrite
4752 a(bort) abort rewriting
4753 r(ule) print out current rewrite rule
4754 s(subst) print out substitution
4755 l(imit) print out rewrite limit count
4756 p(attern) print out stop pattern
4757 stop [<term>] . set(unset) stop pattern
4758 rwt [<number>] .set(unset) max number of rewrite
4759 -- the followings are subset of cafeobj interpreter commands
4760 show -or-
4761 describe print various info., for further help, type `show ?'
4762 set set toplevel switches, for further help: type `set ?'
4763 cd <directory> change current directory
4764 ls <directory> list files in directory
4765 pwd print current directory
4766 lisp -or-
4767 lispq <lisp> evaluate lisp expression <lisp>
4768 ! <command> fork shell <command>. Under Unix only
4769 STEP[1]?
4770 \end{ccode}
4771 \end{vvtm}
4772 Some of them, such as \verb|show|, \verb|set|, and \verb|cd|, should be
4773 familiar. They are not particular to the step mode. Particular to the
4774 mode are the commands prefixed by ``\verb|:|'', in addition to
4775 \verb|rwt| and \verb|stop|.
4776
4777 The prefix ``\verb|:|'' may be omitted, and there are longer forms
4778 for some step mode commands. Here is a summary of synonyms.
4779 \index{\texttt{:h}}\index{\texttt{?}}\index{\texttt{h}}
4780 \index{\texttt{:help}}\index{\texttt{help}}
4781 \index{\texttt{:n}}\index{\texttt{n}}\index{\texttt{:next}}\index{\texttt{next}}
4782 \index{\texttt{:g}}\index{\texttt{g}}\index{\texttt{:go}}\index{\texttt{go}}
4783 \index{\texttt{:c}}\index{\texttt{c}}\index{\texttt{:continue}}\index{\texttt{continue}}
4784 \index{\texttt{:q}}\index{\texttt{q}}
4785 \index{\texttt{:a}}\index{\texttt{a}}\index{\texttt{:abort}}\index{\texttt{abort}}
4786 \index{\texttt{:r}}\index{\texttt{r}}\index{\texttt{:rule}}\index{\texttt{rule}}
4787 \index{\texttt{:s}}\index{\texttt{s}}\index{\texttt{:subst}}\index{\texttt{subst}}
4788 \index{\texttt{:l}}\index{\texttt{l}}\index{\texttt{:limit}}\index{\texttt{limit}}
4789 \index{\texttt{:p}}\index{\texttt{p}}\index{\texttt{:pattern}}\index{\texttt{pattern}}
4790 \index{\texttt{:rew}}\index{\texttt{rew}}\index{\texttt{:rewrite}}\index{\texttt{rewrite}}
4791 \index{\texttt{:stop}}\index{\texttt{stop}}
4792 \begin{center}
4793 \begin{tabular}{llll}
4794 \verb|:?|, \verb|:h| & \verb|?|, \verb|h| & \verb|:help| & \verb|help| \\
4795 \verb|:n| & \verb|n| & \verb|:next| & \verb|next| \\
4796 \verb|:g| & \verb|g| & \verb|:go| & \verb|go| \\
4797 \verb|:c| & \verb|c| & \verb|:continue| & \verb|continue| \\
4798 \verb|:q| & \verb|q| & & \\
4799 \verb|:a| & \verb|a| & \verb|:abort| & \verb|abort| \\
4800 \verb|:r| & \verb|r| & \verb|:rule| & \verb|rule| \\
4801 \verb|:s| & \verb|s| & \verb|:subst| & \verb|subst| \\
4802 \verb|:l| & \verb|l| & \verb|:limit| & \verb|limit| \\
4803 \verb|:p| & \verb|p| & \verb|:pattern| & \verb|pattern| \\
4804 \verb|:rwt| & \verb|rwt| & \verb|:rewrite| & \verb|rewrite| \\
4805 & & \verb|:stop| & \verb|stop| \\
4806 \end{tabular}
4807 \end{center}
4808
4809 \begin{warning}
4810 The command names are tentative, and are subject to change.
4811 \end{warning}
4812
4813 \subsection{Evaluation Under the Step Mode}\label{sec:p2-eval-step-mode}
4814
4815 Continuing the session in the previous section, we illustrate the
4816 effects of each command.\index{\texttt{:n}}
4817 \begin{vvtm}
4818 \begin{ccode}
4819 STEP[1]? :n
4820
4821 >> stepper term: s(s(s(0))) + (s(0) * s(s(s(0))))
4822 STEP[2]? :n
4823
4824 >> stepper term: s(s(0)) + (s(0) * s(s(s(0))))
4825 STEP[3]? :n
4826 >> stepper term: s(0) + (s(0) * s(s(s(0))))
4827 STEP[4]?
4828 \end{ccode}
4829 \end{vvtm}
4830 ``\verb|:n|'' prints a kind of traces, but beware that the printed term
4831 is a subterm under replacement. You may print the current form of
4832 the entire term as\index{\texttt{show}}\index{\texttt{term}}
4833 \begin{vvtm}
4834 \begin{ccode}
4835 STEP[4]? show term
4836 s(s((s(0) + (s(0) * s(s(s(0))))))) : NzNat
4837 STEP[4]?
4838 \end{ccode}
4839 \end{vvtm}
4840 Or you may get a better (if longer) trace if
4841 that is what you want, by setting ``\verb|trace whole|'' to \verb|on|.
4842 Starting from the first step, the output is
4843 \begin{vvtm}
4844 \begin{ccode}
4845 MULT> set trace whole on
4846
4847 MULT> reduce s(s(0)) * s(s(s(0))) .
4848
4849 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4850 >> stepper term: s(s(0)) * s(s(s(0)))
4851 STEP[1]? :n
4852
4853 [1]: s(s(0)) * s(s(s(0)))
4854 ---> s(s(s(0))) + (s(0) * s(s(s(0))))
4855 >> stepper term: s(s(s(0))) + (s(0) * s(s(s(0))))
4856 STEP[2]? :n
4857
4858 [2]: s(s(s(0))) + (s(0) * s(s(s(0))))
4859 ---> s(s(s(0)) + (s(0) * s(s(s(0)))))
4860 >> stepper term: s(s(0)) + (s(0) * s(s(s(0))))
4861 STEP[3]? :n
4862
4863 [3]: s(s(s(0)) + (s(0) * s(s(s(0)))))
4864 ---> s(s(s(0) + (s(0) * s(s(s(0))))))
4865 >> stepper term: s(0) + (s(0) * s(s(s(0))))
4866 STEP[4]?
4867 \end{ccode}
4868 \end{vvtm}
4869 which shows the stepper is focusing more and more inside. To skip
4870 several steps, use \verb|:g|.\index{\texttt{:g}}
4871 \begin{vvtm}
4872 \begin{ccode}
4873 STEP[4]? set trace whole off
4874
4875 STEP[4]? :g 5
4876
4877 >> stepper term: s(0) + (0 * s(s(s(0))))
4878 STEP[9]? :g 5
4879
4880 s(s(s(s(s(s(0)))))) : NzNat
4881 (0.010 sec for parse, 11 rewrites(0.040 sec), 19 matches)
4882 MULT>
4883 \end{ccode}
4884 \end{vvtm}
4885 \verb|show| command may take arguments \verb|term| {\em and} \verb|tree|.
4886 Then a tree form of the term is printed
4887 (See Section \ref{sec:p2-choose-command} for an example).
4888
4889 The rest of the commands act as follows.
4890 \begin{vvtm}
4891 \begin{ccode}
4892 MULT> reduce s(s(0)) * s(s(s(0))) .
4893
4894 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4895 >> stepper term: s(s(0)) * s(s(s(0)))
4896 STEP[1]? :a
4897
4898 s(s(0)) * s(s(s(0))) : Nat
4899 (0.000 sec for parse, 1 rewrites(0.000 sec), 2 matches)
4900 MULT>
4901 \end{ccode}
4902 \end{vvtm}
4903 ``\verb|:a|'' stops the evaluation immediately.\index{\texttt{:a}}
4904 \begin{vvtm}
4905 \begin{ccode}
4906 MULT> reduce s(s(0)) * s(s(s(0))) .
4907
4908 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4909 >> stepper term: s(s(0)) * s(s(s(0)))
4910 STEP[1]? :n
4911
4912 >> stepper term: s(s(s(0))) + (s(0) * s(s(s(0))))
4913 STEP[2]? :r
4914 (s(N:Nat) + N':Nat) = s((N:Nat + N':Nat))
4915 STEP[2]? :s
4916 { N':Nat |-> s(0) * s(s(s(0))), N |-> s(s(0)) }
4917 STEP[2]? :c
4918
4919 s(s(s(s(s(s(0)))))) : NzNat
4920 (0.000 sec for parse, 11 rewrites(0.010 sec), 19 matches)
4921 MULT>
4922 \end{ccode}
4923 \end{vvtm}
4924 ``\verb|:r|'' and ``\verb|:s|'' show the
4925 rule and the substitution to be used in
4926 the {\em next} step (study the above example carefully). ``\verb|:c|''
4927 continues the evaluation non-stop.
4928 \index{\texttt{:r}}\index{\texttt{:s}}\index{\texttt{:c}}
4929
4930 \begin{vvtm}
4931 \begin{ccode}
4932 MULT> reduce s(s(0)) * s(s(s(0))) .
4933
4934 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4935 >> stepper term: s(s(0)) * s(s(s(0)))
4936 STEP[1]? :q
4937
4938 s(s(s(s(s(s(0)))))) : NzNat
4939 (0.000 sec for parse, 11 rewrites(0.010 sec), 19 matches)
4940 MULT> reduce s(s(0)) * s(0) .
4941
4942 -- reduce in MULT : s(s(0)) * s(0)
4943 s(s(0)) : NzNat
4944 (0.000 sec for parse, 7 rewrites(0.000 sec), 11 matches)
4945 MULT>
4946 \end{ccode}
4947 \end{vvtm}
4948 ``\verb|:q|'' forces the system to leave the step mode, as the above
4949 example shows.\index{\texttt{:q}}
4950
4951 For ``\verb|:l|'' and ``\verb|:p|'', see the next section.
4952
4953 \subsection{Controlled Reduction, by Patterns}
4954 \label{sec:p2-control-reduction-pat}
4955
4956 It is possible to stop reductions when a certain pattern is
4957 found.
4958
4959 \begin{bsyntax} \texttt{stop} command \index{\texttt{stop}} \Hline
4960 \texttt{stop} \textit{term} ``\texttt{.}''
4961 \texttt{stop} ``\texttt{.}''
4962 \end{bsyntax}
4963
4964 where a term is a term in the current module context
4965 (Section \ref{sec:p2-current-module}), and may contain variables.
4966 Do not forget the last blank-period stopper. This command causes
4967 reductions to stop when the reductants get to containing subterms
4968 that match the given term.
4969 If no term is given, this restriction is lifted.
4970 Alternatively, you may use \verb|set|\index{\texttt{set}} command,
4971 as\index{\texttt{stop}}\index{\texttt{pattern}}
4972 \begin{vvtm}
4973 \begin{ccode}
4974 CafeOBJ> set stop pattern t .
4975 \end{ccode}
4976 \end{vvtm}
4977 where \verb|t| is a term.
4978
4979 \begin{warning}
4980 This command is fast becoming obsolete, and it is advisable to
4981 use the above \verb|set| command instead.
4982 \end{warning}
4983
4984 We continue the session with \verb|MULT| (Note that the \verb|step|
4985 switch is still \verb|on|).\index{\texttt{:p}}
4986 \begin{vvtm}
4987 \begin{ccode}
4988 MULT> set stop pattern 0 * N:Nat .
4989
4990 MULT> reduce s(s(0)) * s(s(s(0))) .
4991
4992 -- reduce in MULT : s(s(0)) * s(s(s(0)))
4993 >> stepper term: s(s(0)) * s(s(s(0)))
4994 STEP[1]? :p
4995
4996 [stop pattern]: (0 * N:Nat)
4997 STEP[1]? :c
4998
4999 >> subterm : 0 * s(s(s(0)))
5000 of term : s(s(s(s(s(s(0))) + (0 * s(s(s(0)))))))
5001 matches to stop pattern: 0 * N
5002 << will stop rewriting
5003 >> stop because matches stop pattern.
5004 >> stepper term: s(s(s(0))) + (0 * s(s(s(0))))
5005 STEP[7]? set stop pattern .
5006
5007 STEP[7]? :c
5008
5009 s(s(s(s(s(s(0)))))) : NzNat
5010 (0.010 sec for parse, 11 rewrites(0.030 sec), 19 matches)
5011 MULT>
5012 \end{ccode}
5013 \end{vvtm}
5014 ``\verb|:p|'' prints the term given by \verb|set| command, and
5015 ``\verb|:c|'' resumes reduction. The system stopped when a matching
5016 term appeared, as requested. After the restriction was lifted, the
5017 reduction continued uninterrupted.
5018
5019 You may print the current pattern as\index{\texttt{show}}\index{\texttt{stop}}
5020 \begin{vvtm}
5021 \begin{ccode}
5022 STEP[1]? show stop
5023 [stop pattern]: (0 * N:Nat)
5024 STEP[1]?
5025 \end{ccode}
5026 \end{vvtm}
5027 instead of \verb|:p|. This command can be invoked without entering
5028 the stepper. In fact, pattern restrictions may be used outside the
5029 stepper. For example,\index{\texttt{show}}\index{\texttt{stop}}
5030 \begin{vvtm}
5031 \begin{ccode}
5032 MULT> set step off
5033
5034 MULT> set stop pattern 0 * N:Nat .
5035
5036 MULT> show stop
5037 [stop pattern]: (0 * N:Nat)
5038
5039 MULT> reduce s(s(0)) * s(s(s(0))) .
5040
5041 -- reduce in MULT : s(s(0)) * s(s(s(0)))
5042 >> subterm : 0 * s(s(s(0)))
5043 of term : s(s(s(s(s(s(0))) + (0 * s(s(s(0)))))))
5044 matches to stop pattern: 0 * N
5045 << will stop rewriting
5046 s(s(s(s(s(s(0))) + (0 * s(s(s(0))))))) : NzNat
5047 (0.000 sec for parse, 6 rewrites(0.010 sec), 12 matches)
5048 MULT>
5049 \end{ccode}
5050 \end{vvtm}
5051 The pattern restriction forced the reduction to terminate.
5052 As shown above, \verb|show| command with \verb|stop| argument prints
5053 the specified pattern. You should use this command, instead of ``\verb|:p|''
5054 (the latter is recognised only within the stepper).
5055
5056 \begin{warning}
5057 It is not possible to specify more than one patterns, or
5058 more complicated patterns using e.g. regular expressions.
5059 If demands are strong, the future versions may enhance
5060 the pattern description facility.
5061 \end{warning}
5062
5063 \subsection{Controlled Reduction, by Number of Steps}
5064 \label{sec:p2-control-reduction-limit}
5065
5066 It is possible to control reductions by limiting rewrite steps.
5067 Enter the stepper again.\index{\texttt{rwt}}\index{\texttt{limit}}
5068 \begin{vvtm}
5069 \begin{ccode}
5070 MULT> set step on
5071
5072 MULT> set stop pattern .
5073
5074 MULT> reduce s(s(0)) * s(s(s(0))) .
5075
5076 -- reduce in MULT : s(s(0)) * s(s(s(0)))
5077 >> stepper term: s(s(0)) * s(s(s(0)))
5078 STEP[1]? rwt 10
5079
5080 STEP[1]? :c
5081
5082 >> aborting rewrite due to rewrite count limit (= 10) <<
5083 s(s(s(s(s(s(0 * s(s(s(0))))))))) : NzNat
5084 (0.000 sec for parse, 10 rewrites(0.020 sec), 18 matches)
5085 MULT>
5086 \end{ccode}
5087 \end{vvtm}
5088 \verb|rwt| command in the stepper forced the reduction to abort,
5089 after 10 rewrite steps. Note that, unlike ``\verb|:g|'' command,
5090 the system aborted the reduction, not just suspended it.
5091
5092 It is possible to limit rewrite steps outside the stepper.
5093 \index{\texttt{set}}\index{\texttt{rwt}}\index{\texttt{limit}}
5094 \begin{vvtm}
5095 \begin{ccode}
5096 MULT> set step off
5097
5098 MULT> set rwt limit 5
5099
5100 MULT> reduce s(s(0)) * s(s(s(0))) .
5101
5102 -- reduce in MULT : s(s(0)) * s(s(s(0)))
5103 >> aborting rewrite due to rewrite count limit (= 5) <<
5104 s(s(s(s(0) * s(s(s(0)))))) : NzNat
5105 (0.000 sec for parse, 5 rewrites(0.000 sec), 9 matches)
5106 MULT>
5107 \end{ccode}
5108 \end{vvtm}
5109 You should use \verb|set| command, instead of \verb|rwt| command
5110 (the latter is recognised only in the stepper). To lift the
5111 restriction, set the limit to \verb|.|\index{\texttt{show}}\index{\texttt{limit}}
5112 \begin{vvtm}
5113 \begin{ccode}
5114 MULT> set rwt limit .
5115
5116 MULT> show limit
5117 [rewrite limit]: not specified.
5118
5119 MULT> reduce s(s(0)) * s(s(s(0))) .
5120
5121 -- reduce in MULT : s(s(0)) * s(s(s(0)))
5122 s(s(s(s(s(s(0)))))) : NzNat
5123 (0.000 sec for parse, 11 rewrites(0.000 sec), 19 matches)
5124 MULT>
5125 \end{ccode}
5126 \end{vvtm}
5127 As shown above, \verb|show| command with \verb|limit| argument
5128 prints the specified limit.
5129
5130 If both the pattern and limit restrictions are in force,
5131 the system aborts reductions at the earliest opportunity.
5132
5133 \section{Faster Evaluation}\label{sec:p2-tram-brute}
5134
5135 Such commands as \verb|reduce| treat terms and rewrite rules as
5136 Lisp objects, and the rewrite procedure manipulates these objects
5137 directly. If the rule set is huge, and/or the term is or will grow
5138 large, the system may become uncomfortably slow. A way to overcome
5139 the discomfort is to invest in a faster machine with a larger memory,
5140 of course, but there is another, cheaper way. For faster evaluation,
5141 you may encode rewrite rules into an abstract machine, and
5142 reduce terms by executing that machine, with the following command.
5143
5144 \begin{bsyntax} \texttt{tram} command for evaluation\index{\texttt{tram}}\index{\texttt{reduce}} \Hline
5145 \index{\texttt{execute}}
5146 \texttt{tram} $\{$ \texttt{reduce} $|$ \texttt{execute} $\}$ $[$\texttt{in} \textit{module\_expression} ``\texttt{:}''$]$ \textit{term} ``\texttt{.}''
5147 \end{bsyntax}
5148
5149 \verb|reduce|, \verb|execute| may be abbreviated to \verb|red|, \verb|exec|
5150 respectively.
5151 The command acts like \verb|reduce| or \verb|execute| and reduces
5152 the given term. If the module expression is omitted, the reduction
5153 occurs in the current module.
5154
5155 \begin{warning}
5156 The compiled code does not distinguish behavioural axioms as such,
5157 and \verb|reduce| option acts like \verb|breduce| command.
5158 \end{warning}
5159
5160 Upon accepting the command, the system
5161 \begin{itemize}
5162 \item[1.] Compiles the rewrite rules of the module into an abstract
5163 machine, if they have not been compiled already; Then it
5164 \item[2.] Runs the abstract machine on the given term.
5165 \end{itemize}
5166 The output you get is more or less the same as when \verb|reduce| or
5167 \verb|execute| commands are used. For example, with \verb|MULT|
5168 (Section \ref{exs:mult}), you have
5169 \begin{vvtm}
5170 \begin{ccode}
5171 MULT> tram reduce s(s(s(0))) * (s(s(s(s(s(0))))) * s(s(s(s(s(0)))))) .
5172 -- reduce in MULT : s(s(s(0))) * (s(s(s(s(s(0))))) * s(s(s(s(s(0)
5173 )))))
5174 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5175 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5176 s(s(s(s(s(s(s(s(s(s(s(0))))))))))))))))))))))))))))))))))))))
5177 ))))))))))))))))))))))))))))))))))))) : NzNat
5178 (rtime 3 utime 3 stime 0 rewrite 118 gc 0)
5179
5180 MULT>
5181 \end{ccode}
5182 \end{vvtm}
5183 For comparison, the corresponding ordinary evaluation works as follows.
5184 \begin{vvtm}
5185 \begin{ccode}
5186 MULT> reduce s(s(s(0))) * (s(s(s(s(s(0))))) * s(s(s(s(s(0)))))) .
5187
5188 -- reduce in MULT : s(s(s(0))) * (s(s(s(s(s(0))))) * s(s(s(s(s(0)
5189 )))))
5190 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5191 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5192 s(s(s(s(s(s(s(s(s(s(0))))))))))))))))))))))))))))))))))))))))))
5193 ))))))))))))))))))))))))))))))))) : NzNat
5194 (0.010 sec for parse, 118 rewrites(0.020 sec), 226 matches)
5195 MULT>
5196 \end{ccode}
5197 \end{vvtm}
5198 Compilation is done only once, when \verb|tram| command is first used
5199 on the given module.
5200
5201 You may just compile a module as a preparation, by
5202
5203 \begin{bsyntax} \texttt{tram} command for compilation \index{\texttt{tram}} \Hline
5204 \index{\texttt{compile}}
5205 \texttt{tram} \texttt{compile} $[$\texttt{-exec}$]$ $[$\textit{module\_expression}$]$ ``\texttt{.}''
5206 \end{bsyntax}
5207
5208 If the module expression is omitted, the current module is assumed.
5209 If \verb|-exec| is given, transitions as well as equations are
5210 compiled (i.e., for \verb|exec|). For this command,
5211 \verb|-e|, \verb|-all|, and \verb|-a| are all synonymous to \verb|-exec|.
5212
5213 \begin{warning}
5214 \verb|reduce| and \verb|execute| use different sets of rules.
5215 Therefore, if you have compiled a module already for \verb|reduce| and
5216 try \verb|tram| with \verb|execute| option, the system recompile
5217 the module.
5218 \end{warning}
5219
5220 \begin{warning}
5221 Do not forget the last period preceded by a blank.
5222 \end{warning}
5223
5224 Compilation into and execution of abstract machines are taken care of
5225 by an indepent program, called \verb|tram| or \verb|brute|. So you have to
5226 tell the system where it is. The command\index{\texttt{switch}}
5227 \begin{vvtm}
5228 \begin{ccode}
5229 CafeOBJ> set tram path /home/irene/bin/tram
5230 \end{ccode}
5231 \end{vvtm}
5232 directs the system to the given path for this program. The default is
5233 \begin{vvtm}
5234 \begin{ccode}
5235 /usr/local/bin/tram
5236 \end{ccode}
5237 \end{vvtm}
5238 \verb|tram| and \verb|brute| have quite different architectures and
5239 they both have limitations. For detail, see README in the distribution
5240 package.
5241
5242 There is another switch related to \verb|tram|, although
5243 you may never use it. The command
5244 \begin{vvtm}
5245 \begin{ccode}
5246 CafeOBJ> set tram options someoptionsoftram
5247 \end{ccode}
5248 \end{vvtm}
5249 makes the system supply \verb|someoptionsoftram| as options whenever
5250 \verb|tram| is invoked. In normal
5251 usage, you need not know which options \verb|tram| or \verb|brute| takes.
5252
5253 \section{Context Variable}\label{sec:p2-let-command}
5254
5255 To help ease reading and writing terms, a {\em context variable}
5256 \index{context variable} facility is supported.
5257
5258 \begin{bsyntax} \texttt{let} command \index{\texttt{let}} \Hline
5259 \texttt{let} \textit{identifier} ``\texttt{=}'' term ``\texttt{.}''
5260 \end{bsyntax}
5261
5262 The last period preceded by a blank is a must. An identifier
5263 is a character string and a term is a term in the current context
5264 (Section {sec:p2-current-module}).
5265 This command binds the given term to the identifier in the current
5266 context. The example below illustrates the usage, and shows how
5267 to print the binding state.
5268 \begin{vvtm}
5269 \begin{ccode}
5270 MULT> let a1 = s(0) + (s(s(0)) + s(s(s(0)))) .
5271 -- setting let variable "a1" to :
5272 (s(0) + (s(s(0)) + s(s(s(0))))) : Nat
5273
5274 MULT> let a2 = s(s(s(0))) * (a1 * s(s(s(s(s(0)))))) .
5275 -- setting let variable "a2" to :
5276 (s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0))))) * s(s(s(s(s(0)))))))
5277 : Nat
5278
5279 MULT> reduce a1 .
5280
5281 -- reduce in MULT : s(0) + (s(s(0)) + s(s(s(0))))
5282 s(s(s(s(s(s(0)))))) : NzNat
5283 (0.000 sec for parse, 5 rewrites(0.010 sec), 8 matches)
5284 MULT> reduce a2 .
5285
5286
5287 -- reduce in MULT : s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0)))))
5288 * s(s(s(s(s(0))))))
5289 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5290 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5291 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0))))))))))))
5292 )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
5293 ))))))))))))))) : NzNat
5294 (0.000 sec for parse, 145 rewrites(0.010 sec), 277 matches)
5295 MULT> show term a1
5296 (s(0) + (s(s(0)) + s(s(s(0))))) : Nat
5297
5298 MULT>
5299 \end{ccode}
5300 \end{vvtm}
5301 A context variable, once bound, may be used as a subterm in
5302 commands \verb|reduce|, \verb|parse|, \verb|let| itself, etc.
5303 Note that \verb|let| binds syntactically.
5304 \verb|reduce| commands above did not change the values of
5305 \verb|a1| or \verb|a2|. \verb|show| command was used to
5306 print the binding of \verb|a1|. To list all the bindings, use
5307 \verb|show| command again:
5308 \index{\texttt{show}}\index{\texttt{let}}\index{\texttt{binding}}
5309 \begin{vvtm}
5310 \begin{ccode}
5311 MULT> show let
5312 [bindings]
5313 a2 = s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0))))) * s(s(s(s(s(0))))))
5314 a1 = s(0) + (s(s(0)) + s(s(s(0))))
5315
5316 MULT>
5317 \end{ccode}
5318 \end{vvtm}
5319 \verb|let| in the above command may be replaced by \verb|binding|.
5320
5321 Context variables are local to each context. Continuing the above session,
5322 \begin{vvtm}
5323 \begin{ccode}
5324 MULT> select SIMPLE-NAT
5325
5326 SIMPLE-NAT> let a1 = 0 .
5327 -- setting let variable "a1" to :
5328 0 : Zero
5329
5330 SIMPLE-NAT> show let
5331 [bindings]
5332 a1 = 0
5333
5334 SIMPLE-NAT> select MULT
5335
5336 MULT> show let
5337 [bindings]
5338 a2 = s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0))))) * s(s(s(s(s(0))))))
5339 a1 = s(0) + (s(s(0)) + s(s(s(0))))
5340
5341 MULT>
5342 \end{ccode}
5343 \end{vvtm}
5344 As shown above, context variables in \verb|MULT| and \verb|SIMPLE-NAT|
5345 are kept separately, and the same name may be used for different
5346 bindings.
5347
5348 \begin{warning}
5349 As explained, context variables belong to individual modules. If you
5350 attempt to bind a context variable without setting a current module,
5351 the system complains --- it has no way to know whither the term came.
5352 \end{warning}
5353
5354 By the way, you encountered not so small a term when \verb|a2| was
5355 reduced. It is very simple for the system to print such terms accurately,
5356 they may sometimes cause you nausea or headache. If you expect
5357 the terms to be large, and need to see only their broad outline,
5358 you can use a switch.
5359 \index{\texttt{print}}\index{\texttt{depth}}
5360 \begin{vvtm}
5361 \begin{ccode}
5362 CafeOBJ> set print depth 10
5363 \end{ccode}
5364 \end{vvtm}
5365 tells the system to print terms up to the depth 10 (as trees).
5366 The default is ``unlimited'', and the depth of ``\verb|.|'' is
5367 interpreted as such. Continuing the above session, then, you get
5368 \begin{vvtm}
5369 \begin{ccode}
5370 MULT> set print depth 10
5371
5372 MULT> reduce a2 .
5373
5374 -- reduce in MULT : s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0)))))
5375 * s(s(s(s(s(0))))))
5376 s(s(s(s(s(s(s(s(s(s( ... )))))))))) : NzNat
5377 (0.000 sec for parse, 145 rewrites(0.010 sec), 277 matches)
5378 MULT> set print depth .
5379
5380 MULT> reduce a2 .
5381
5382 -- reduce in MULT : s(s(s(0))) * ((s(0) + (s(s(0)) + s(s(s(0)))))
5383 * s(s(s(s(s(0))))))
5384 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5385 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(
5386 s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0))))))))))))
5387 )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
5388 ))))))))))))))) : NzNat
5389 (0.000 sec for parse, 145 rewrites(0.010 sec), 277 matches)
5390 MULT>
5391 \end{ccode}
5392 \end{vvtm}
5393
5394 \section{Flexible Typing and Error Handling}\label{sec:p2-flexible-typing}
5395
5396 \cafeobj~has a strong typing discipline. Although useful for static
5397 analyses, strong typing is sometimes too restrictive. There are cases
5398 where type mismatch could never happen, even though expressions are
5399 syntactically illegal. Or, rephrasing in the lexicon of TRS's,
5400 apparently ill-formed terms may reduce to well-formed terms.
5401 \cafeobj~appraises such phenomena, to offer
5402 a ``reasonably'' strong typing discipline.
5403
5404 Consider a case when you try to apply an operator
5405 \begin{vvtm}
5406 \begin{ccode}
5407 op f : S -> S'
5408 \end{ccode}
5409 \end{vvtm}
5410 to a term of sort \verb|T|. \verb|S| and \verb|T| differ, so such an
5411 attempt should be rejected. Unless, that is, the relation
5412 \begin{vvtm}
5413 \begin{ccode}
5414 S < T
5415 \end{ccode}
5416 \end{vvtm}
5417 holds. Note well that, if
5418 \begin{vvtm}
5419 \begin{ccode}
5420 T < S
5421 \end{ccode}
5422 \end{vvtm}
5423 holds, \verb|f| can definitely apply to terms of \verb|T|. That is
5424 an essence of order-sorted logic. What we are saying here is that,
5425 if \verb|S| is a subsort of \verb|T| and a term \verb|t| is
5426 of sort \verb|T|, \verb|f(t)|
5427 may become (by evaluation) a well-formed term, since \verb|t| may
5428 reduce to a term \verb|t'| of sort \verb|S|.
5429
5430 One way to accommodate the above case is to use so-called retract operators
5431 (c.f. \cite{OBJ}).
5432 \cafeobj~has adopted another approach. Let us see how it works.
5433
5434 \subsection{Factorial of Rationals ($!?$)}
5435
5436 A usual definition of the factorial is as follows.
5437 \label{exs:fact}
5438 \begin{vvtm}
5439 \begin{ccode}
5440 module FACT {
5441 protecting (RAT)
5442 op _! : Nat -> NzNat
5443 eq 0 ! = 1 .
5444 ceq N:Nat ! = N * (N - 1 !) if N > 0 .
5445 }
5446 \end{ccode}
5447 \end{vvtm}
5448 where the factorial is declared as a postfix operator \verb|_!|, as
5449 is the convention. \verb|RAT| is a built-in module of rationals.
5450 You may point out that rationals are irrelevant to the factorial. They
5451 are, and this module is certainly artificial: it is only declared to show
5452 that we can compute the factorial of rationals. Well, sometimes.
5453
5454 The sort ordering in \verb|RAT| defines the graph
5455 \begin{center}
5456 % \begin{diagram}
5457 % &&\mbox\texttt{Rat} \\
5458 % &\ldTo&\dTo \\
5459 % \mbox\texttt{NzRat} && \mbox\texttt{Int} \\
5460 % \dTo&\ldTo&\dTo \\
5461 % \mbox\texttt{NzInt} &&\mbox\texttt{Nat} \\
5462 % \dTo&\ldTo&\dTo\\
5463 % \mbox\texttt{NzNat} &&\mbox\texttt{Zero}
5464 % \end{diagram}
5465 \end{center}
5466 All these sorts are of familiar kind:
5467 \verb|Rat|, \verb|Int|, \verb|Nat| are the sorts of rationals, integers, and
5468 natural numbers respectively, and \verb|Nz| means non-zero. \verb|Zero| is
5469 a one-point set, containing 0. Unlike our \verb|SIMPLE-NAT|, naturals may
5470 be written 1, 43, 107, etc. in this built-in module.
5471
5472 \verb|RAT| defines the division
5473 \begin{vvtm}
5474 \begin{ccode}
5475 op _/_ : Rat NzRat -> Rat
5476 \end{ccode}
5477 \end{vvtm}
5478 Now
5479 \begin{vvtm}
5480 \begin{ccode}
5481 (I:Int / J:Int)!
5482 \end{ccode}
5483 \end{vvtm}
5484 is ill-formed, since \verb|I / J| is a rational, while \verb|_!| takes
5485 only natural number arguments. But \cafeobj~parses it as well-formed,
5486 as follows.
5487 \begin{vvtm}
5488 \begin{ccode}
5489 CafeOBJ> parse in FACT : (I:Int / J:Int)! .
5490
5491 ((I / J) !) : ?Rat
5492 \end{ccode}
5493 \end{vvtm}
5494 Recall that the result of parsing is a pair of a parenthesised term and
5495 its least sort. Here the least sort is shown to be a questionable
5496 \verb|?Rat|. The naming convention is implementation-dependent and
5497 should not bother you, but you should be bothered to know to that
5498 the name denotes an {\em error sort}\index{error sort}
5499 over \verb|Rat|, i.e. a sort consisting
5500 of ``error'' elements as well as rationals.
5501
5502 For any integers \verb|I| and \verb|J|, then, \verb|(I / J)!| is well-formed,
5503 although its sort has a dubious name. It follows that
5504 \begin{vvtm}
5505 \begin{ccode}
5506 (4 / 2)!
5507 \end{ccode}
5508 \end{vvtm}
5509 is well-formed. Try parsing, then evaluating the term, and you get
5510 \begin{vvtm}
5511 \begin{ccode}
5512 CafeOBJ> select FACT
5513
5514 FACT> parse (4 / 2)! .
5515
5516 ((4 / 2) !) : ?Rat
5517 FACT> reduce (4 / 2)! .
5518
5519 -- reduce in FACT : 4 / 2 !
5520 2 : NzNat
5521 (0.000 sec for parse, 14 rewrites(0.020 sec), 24 matches)
5522 FACT>
5523 \end{ccode}
5524 \end{vvtm}
5525 The original term is parsed as a term of sort \verb|?Rat|, as before, and
5526 the result of evaluation is a term \verb|2| of sort \verb|NzNat|.
5527 Indeed, this result is the 2's factorial, i.e. $4/2$'s factorial.
5528 And \verb|NzNat| is not dubious.
5529
5530 On the other hand, if you supply a meaningless term, the result remains
5531 dubious.
5532 \begin{vvtm}
5533 \begin{ccode}
5534 FACT> reduce (4 / 3)! .
5535
5536 -- reduce in FACT : 4 / 3 !
5537 4/3 ! : ?Rat
5538 (0.010 sec for parse, 1 rewrites(0.000 sec), 1 matches)
5539 FACT>
5540 \end{ccode}
5541 \end{vvtm}
5542 To summarise, in \cafeobj,
5543 \begin{itemize}
5544 \item[(1)] If a term is ill-formed but potentially well-formed,
5545 it is regarded as a well-formed term of questionable sort.
5546 \item[(2)] On evaluation, the potential is investigated.
5547 If the result is a term which is unquestionably well-formed,
5548 it acquires a full citizenship. Otherwise it remains an outcast.
5549 \end{itemize}
5550
5551 \subsection{Stacks}
5552
5553 Stacks are ubiquitous topics in our
5554 community, especially in discussing exception/error handling. The
5555 main reason is that their definition is compact, yet reveals (or
5556 conceals) how \verb|top(empty)|, a raving exception, is treated.
5557 The module below declares a sort of non-empty stacks, and
5558 \verb|top| and \verb|pop| are declared on that sort, not on the sort of
5559 all the stacks. In effect, \verb|top| and \verb|pop| are partial
5560 operators.\index{stack}\index{partial operator}\label{exs:stack-of-nat}
5561 \begin{vvtm}
5562 \begin{ccode}
5563 module STACK-OF-NAT {
5564 [ NeStack < Stack ]
5565 protecting (SIMPLE-NAT)
5566 op empty : -> Stack
5567 op push : Nat Stack -> NeStack
5568 op top_ : NeStack -> Nat
5569 op pop_ : NeStack -> Stack
5570 eq top push(X:Nat, S:Stack) = X .
5571 eq pop push(X:Nat, S:Stack) = S .
5572 }
5573 \end{ccode}
5574 \end{vvtm}
5575 ``\verb|top push(s(s(0)), empty)|'' is well-formed. It parses and reduces as
5576 \begin{vvtm}
5577 \begin{ccode}
5578 CafeOBJ> select STACK-OF-NAT
5579
5580 STACK-OF-NAT> parse top push(s(s(0)), empty) .
5581
5582 (top push(s(s(0)),empty)) : Nat
5583 STACK-OF-NAT> reduce top push(s(s(0)), empty) .
5584
5585 -- reduce in STACK-OF-NAT : top push(s(s(0)),empty)
5586 s(s(0)) : NzNat
5587 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
5588 STACK-OF-NAT>
5589 \end{ccode}
5590 \end{vvtm}
5591 Without introducing error sorts, the term
5592 ``\verb|top pop push(s(0), push(s(s(0)), empty))| is ill-formed:
5593 \verb|top| requires non-empty stacks as arguments, but the coarity of
5594 \verb|pop| is \verb|Stack|, which contains the empty stack.
5595 \cafeobj~regards this term,
5596 like \verb|(4 / 2)!|, as well-formed, but of an error sort \verb|?Nat|.
5597 On evaluation, it reduces to a well-formed term of a valid sort.
5598 \begin{vvtm}
5599 \begin{ccode}
5600 STACK-OF-NAT> parse top pop push(s(0), push(s(s(0)), empty)) .
5601
5602 (top (pop push(s(0),push(s(s(0)),empty)))) : ?Nat
5603 STACK-OF-NAT> reduce top pop push(s(0), push(s(s(0)), empty)) .
5604
5605 -- reduce in STACK-OF-NAT : top (pop push(s(0),push(s(s(0)),empty)))
5606 s(s(0)) : NzNat
5607 (0.000 sec for parse, 2 rewrites(0.000 sec), 2 matches)
5608 STACK-OF-NAT>
5609 \end{ccode}
5610 \end{vvtm}
5611 A meaningless term, as before, remains meaningless.
5612 \begin{vvtm}
5613 \begin{ccode}
5614 STACK-OF-NAT> parse top pop push(s(0), empty) .
5615
5616 (top (pop push(s(0),empty))) : ?Nat
5617 STACK-OF-NAT> reduce top pop push(s(0), empty) .
5618
5619 -- reduce in STACK-OF-NAT : top (pop push(s(0),empty))
5620 top empty : ?Nat
5621 (0.010 sec for parse, 1 rewrites(0.000 sec), 1 matches)
5622
5623 STACK-OF-NAT>
5624 \end{ccode}
5625 \end{vvtm}
5626
5627 \subsection{Handling Errors as Errors}
5628
5629 The above definition of natural number stacks does not say much about
5630 what to do with errors. It simply demarcates the error boundary.
5631 If you want to be more specific about handling errors,
5632 a standard way is to declare specific error elements
5633 (constants), and equalise errors to them. In case of
5634 the stacks, the code would look something like
5635 \begin{vvtm}
5636 \begin{ccode}
5637 ...
5638 op * : -> ?Nat
5639 op * : -> ?Stack
5640 ...
5641 eq top empty = * .
5642 eq pop empty = * .
5643 ...
5644 \end{ccode}
5645 \end{vvtm}
5646 where \verb|*| is meant to be a bottom element. A more elaborate
5647 way is to declare such constants as
5648 \begin{vvtm}
5649 \begin{ccode}
5650 op zero-divisor : -> ?Nat
5651 op top-of-empty-stack : -> ?Nat
5652 \end{ccode}
5653 \end{vvtm}
5654 They are intended to be error messages.
5655
5656 An error sort is automatically declared for each connected component of
5657 the sort graph, and has an internal name that starts with ``\verb|?|''.
5658 In the examples of factorials and stacks, the names derived from single
5659 sorts, as \verb|?Rat| and \verb|?Nat|. In general, an error sort may
5660 have a complicated name like
5661 \begin{vvtm}
5662 \begin{ccode}
5663 ?SortA+SortB+SortC
5664 \end{ccode}
5665 \end{vvtm}
5666 The ``summands'' of this name are maximal sorts in a relevant connected
5667 component. In making explicit references, you may use a more compact
5668 notation: just put a question mark before a sort name in connected
5669 components. For example,
5670 if you declared
5671 \begin{vvtm}
5672 \begin{ccode}
5673 [ A < B, A < C ]
5674 \end{ccode}
5675 \end{vvtm}
5676 the error sort name is internally \verb|?B+C|, but may be referred to
5677 as either \verb|?A|, \verb|?B|, or \verb|?C|.
5678
5679 \begin{warning}
5680 Unlike the former versions, the current version admits error sorts,
5681 such as \verb|?Nat|, in operator declarations.
5682 Since names of error sorts --- in fact, their
5683 existence at all --- are implementation dependent,
5684 you should avoid mentioning error sorts unless absolutely necessary.
5685 \end{warning}
5686
5687 \chapter{Operator Attributes}\label{sec:p2-op-properties}
5688
5689 Some properties of operators, such as associativity, can be stated as
5690 axioms, but if they are so common, it is convenient to be able to
5691 use shorthand notations. Moreover, some properties, such as
5692 commutativity, are better {\em not} to be given by axioms,
5693 since, if regarded as rewrite rules, such axioms violate
5694 desirable properties of the TRS. \cafeobj~has a construct for endowing
5695 operators with common properties. Such properties are called
5696 {\em operator attribute\/}s\index{operator attribute}
5697 \index{attribute (of operators)}. Some of them
5698 affect denotational semantics; others affect only parsing; yet others
5699 concern solely with evaluation.
5700
5701 Operator attributes are specified with an optional construct of
5702 operator declarations that were explained in Section \ref{sec:p2-operator}.
5703
5704 \begin{bsyntax} operator declaration with attributes \index{\texttt{op}} \Hline
5705 \texttt{op} \textit{operator\_symbol} ``\texttt{:}'' \textit{list\_of\_sort\_names} ``\texttt{->}'' \textit{sort\_name} ``\texttt{\{}'' \textit{attribute\_list} ``\texttt{\}}''
5706 \end{bsyntax}
5707
5708 where operator symbols et al. are as before (Section \ref{sec:p2-operator}).
5709 We delayed the introduction of this construct, since it is
5710 better explained after evaluation commands.
5711
5712 An attribute list is a list of operator attributes separated by blanks.
5713 An attribute is either (1) an {\em equational theory attribute},
5714 \index{equational theory attribute} which defines an equivalence relation
5715 among terms, (2) a {\em parsing attribute}\index{parsing attribute}, which
5716 specifies connectivity,
5717 (3) an {\em evaluation attribute}, which controls the evaluation
5718 procedure, or (4) neither of these.
5719
5720 Operator declarations introduced by \verb|ops|, \verb|bop|, \verb|bops|,
5721 \verb|pred| may also have this optional construct.
5722
5723 \section{Equational Theory Attribute}\label{sec:p2-equational-theory}
5724 \cafeobj~admits four equational theory attributes, as follows. For
5725 an infix operator $+$,
5726 \begin{itemize}
5727 \item[(1)] Associativity, or $(x + y) + z$ = $x + (y + z)$ for any
5728 $x$, $y$, and $z$. \\
5729 Specified with \verb|associative| or \verb|assoc|.
5730 \index{associativity}\index{\texttt{associative}}\index{\texttt{assoc}}
5731 \item[(2)] Commutativity, or $x + y = y + x$ for any $x$ and $y$. \\
5732 Specified with \verb|commutative| or \verb|comm|.
5733 \index{commutativity}\index{\texttt{commutative}}\index{\texttt{comm}}
5734 \item[(3)] Idempotency, or $x + x = x$ for any $x$. \\
5735 Specified with \verb|idempotent| or \verb|idem|.
5736 \index{idempotency}\index{\texttt{idempotent}}\index{\texttt{idem}}
5737 \item[(4)] Existence of the identity (here written $0$), or
5738 $0 + x = x$ and $x + 0 = x$ for any $x$. \\
5739 Specified with ``\verb|id: 0|''.
5740 \index{identity}\index{\texttt{id:}}
5741 \end{itemize}
5742
5743 \subsection{Associativity}
5744
5745 We proceed to a couple of examples for futher illustration.
5746 The conjunction ``\verb|_and_|'' is associative.
5747 In \cafeobj, it is written as
5748 \index{\texttt{assoc}}
5749 \begin{vvtm}
5750 \begin{ccode}
5751 op _and_ : Bool Bool -> Bool { assoc }
5752 \end{ccode}
5753 \end{vvtm}
5754 which means
5755 \begin{itemize}
5756 \item[-] No ambiguity arises with or without parentheses. For example,
5757 \begin{vvtm}
5758 \begin{ccode}
5759 true and false and true
5760 \end{ccode}
5761 \end{vvtm}
5762 has two parses
5763 \begin{vvtm}
5764 \begin{ccode}
5765 (true and false) and true
5766 true and (false and true)
5767 \end{ccode}
5768 \end{vvtm}
5769 but by associativity, they are equal.
5770 \item[-] During evaluation, terms are matched modulo
5771 \index{modulo (equatioanl theory)}
5772 associativity. For example, in evaluating \newline
5773 ``\verb|(true and false) and true|'' in the presence of
5774 \begin{vvtm}
5775 \begin{ccode}
5776 eq true and X:Bool = X .
5777 \end{ccode}
5778 \end{vvtm}
5779 the given term does not match the lefthand side as it is, since the first
5780 argument is not \verb|true|. But by associativity,
5781 the term equals ``\verb|true and (false and true)|'', and this
5782 equivalent term does match the lefthand side. Matching modulo
5783 associativity is a procedure to find a matching substitution under
5784 arbitrary arrangement of parentheses.
5785 \end{itemize}
5786 The built-in equality operator ``\verb|_==_|''
5787 (Section \ref{sec:p2-equality-pred}) takes care of associativity.
5788 For example,
5789 \begin{vvtm}
5790 \begin{ccode}
5791 true and (false and true) == (true and false) and true
5792 \end{ccode}
5793 \end{vvtm}
5794 evaluates to \verb|true|.
5795
5796 \verb|assoc| makes sense only when the operator is binary,
5797 with arity of the form ``\verb|A A|'' and coarity \verb|C| such that
5798 \verb|C < A|.
5799
5800 \begin{warning}
5801 Formerly, \cafeobj~admitted the cases when the arity
5802 consists of different sorts. You are warned if the above stricter
5803 condition is violated.
5804 \end{warning}
5805
5806 \begin{warning}
5807 The system assumes an operator with \verb|assoc| attribute to
5808 be right associative (Section \ref{sec:p2-syntactic-properties}),
5809 unless declared otherwise.
5810 \end{warning}
5811
5812 \subsection{Commutativity}
5813 The conjunction is also commutative. In \cafeobj, it is written as
5814 \index{\texttt{comm}}
5815 \begin{vvtm}
5816 \begin{ccode}
5817 op _and_ : Bool Bool -> Bool { assoc comm }
5818 \end{ccode}
5819 \end{vvtm}
5820 Commutativity may be specified with the equation
5821 \begin{vvtm}
5822 \begin{ccode}
5823 eq X:Bool and Y:Bool = Y and X .
5824 \end{ccode}
5825 \end{vvtm}
5826 which is fine as an equation. As a rewrite rule, however, it is
5827 diabolically problematic, since it generates infinite rewrite sequences.
5828 For this reason, commutativity should be stated by operator attribute.
5829
5830 Like \verb|assoc|, \verb|comm| affects evaluation. For example, the
5831 term ``\verb|false and true|'' does not match the lefthand side of
5832 \begin{vvtm}
5833 \begin{ccode}
5834 eq X:Bool and false = false .
5835 \end{ccode}
5836 \end{vvtm}
5837 but an equivalent term \verb|true and false| does.
5838
5839 ``\verb|_==_|'' takes care of commutativity as well as associativity, so
5840 \begin{vvtm}
5841 \begin{ccode}
5842 true and (false and true) == (true and true) and false
5843 \end{ccode}
5844 \end{vvtm}
5845 evaluates to \verb|true|.
5846
5847 \verb|comm| makes sense only when the operator is binary and its
5848 arity sorts are the same.
5849
5850 \begin{warning}
5851 Formerly, \cafeobj~admitted the cases when
5852 the arity sorts have a common supersort.
5853 \end{warning}
5854
5855 \subsection{Identity}
5856 ``\verb|_and_|'' has \verb|true| as (left and right) identity, which is
5857 written in \cafeobj~as\index{\texttt{id:}}
5858 \begin{vvtm}
5859 \begin{ccode}
5860 op _and_ : Bool Bool -> Bool { assoc comm id: true }
5861 \end{ccode}
5862 \end{vvtm}
5863 The attribute ``\verb|id: true|'' is equivalent to the following equations.
5864 \begin{vvtm}
5865 \begin{ccode}
5866 eq true and X:Bool = X .
5867 eq X:Bool and true = X .
5868 \end{ccode}
5869 \end{vvtm}
5870 (In fact, since \verb|comm| is also given, one of the above is enough.)
5871 Identity attributes affect evaluation. Under the above operator
5872 declaration, \verb|false and true| is, {\em without} evaluation,
5873 identified with \verb|false|.
5874
5875 ``\verb|id:|'' makes sense only when the operator is binary,
5876 the arity sorts and the coarity sort are the same, and the identity term
5877 belongs to that sort.
5878
5879 \begin{warning}
5880 Formerly, \cafeobj
5881 admitted the cases when the coarity sort is different.
5882 % The current implementation does not check the above condition.
5883 \end{warning}
5884
5885 \begin{warning}
5886 The current implementation of \cafeobj~applies a completion procedure
5887 to realise matching modulo identity. The procedure often generates a
5888 great many of equations, leading to severe performace degeneration,
5889 or even to non-termination. To prohibit unwanted completion,
5890 use ``\verb|idr:|''\index{\texttt{idr:}}, instead of ``\verb|id:|''.
5891 This attribute has the the same meaning as ``\verb|id:|''.
5892 \end{warning}
5893
5894 \subsection{Idempotency}
5895
5896 That ``\verb|_and_|'' is idempotent is stated as follows.\index{\texttt{idem}}
5897 \begin{vvtm}
5898 \begin{ccode}
5899 op _and_ : Bool Bool -> Bool { assoc comm id: true idem }
5900 \end{ccode}
5901 \end{vvtm}
5902 \verb|idem| is equivalent to the equation
5903 \begin{vvtm}
5904 \begin{ccode}
5905 eq X:Bool and X = X .
5906 \end{ccode}
5907 \end{vvtm}
5908 Idempotency affects evaluation. For example,
5909 ``\verb|true and true|'' and \verb|true| matches modulo idempotency.
5910
5911 \verb|idem| makes sense only when the operator is binary and the
5912 arity sorts and the coarity sort are the same.
5913
5914 \begin{warning}
5915 Formerly there was no explicit condition.
5916 \end{warning}
5917
5918 \begin{warning}
5919 In the current implementation, an equation like the above is
5920 actually added. The system sometimes misbehaves; the only certainty
5921 about idempotency is that matching modulo the {\em combination} of
5922 associativity, commutativity, and idempotency works correctly.
5923 \end{warning}
5924
5925 \subsection{Inheriting Equational Theory Attributes}
5926
5927 Equational theory attributes are
5928 inherited by operators, if they have the same name and are
5929 declared on lower sorts. For example, when you declare
5930 \begin{vvtm}
5931 \begin{ccode}
5932 [ Nat < Int ]
5933 op _+_ : Nat Nat -> Nat
5934 op _+_ : Int Int -> Int { assoc }
5935 \end{ccode}
5936 \end{vvtm}
5937 the operator ``\verb|_+_|'' on \verb|Nat| is assumed associative.
5938 Since a subsort is a subset, such attribute inheritance is quite natural:
5939 if associativity holds for all the integers, it a forteriori holds for
5940 all the natural numbers.
5941 As a special case, an identity is inherited only when the identity
5942 term belongs to the lower sort.
5943
5944 \section{Parsing Attribute}\label{sec:p2-syntactic-properties}
5945
5946 Parsing attributes help disambiguate parsing. They are either
5947 \begin{itemize}
5948 \item[(1)] Precedence\index{precedence}, specified by
5949 ``\verb|prec:| integer'', or
5950 \index{\texttt{prec:}}
5951 \item[(2)] Left or right associativity, specified by \verb|l-assoc| or
5952 \verb|r-assoc|, respectively.
5953 \index{\texttt{r-assoc}}\index{\texttt{l-assoc}}
5954 \index{left associativity}\index{right associativity}
5955 \end{itemize}
5956
5957 \subsection{Precedence}
5958
5959 A precedence is an integer, and lesser precedences indicate stronger
5960 connectivity. For example,\index{\texttt{prec:}}
5961 \begin{vvtm}
5962 \begin{ccode}
5963 op _+_ : Int Int -> Int { prec: 33 }
5964 op _*_ : Int Int -> Int { prec: 31 }
5965 \end{ccode}
5966 \end{vvtm}
5967 states that ``\verb|_*_|'' connects stronger than ``\verb|_+_|'', and
5968 ``\verb|1 + 2 * 3|'' parses as ``\verb|1 + (2 * 3)|''.
5969
5970 The default precedences depend on term constructs.
5971 \begin{itemize}
5972 \item Standard operators have precedence 0. Hence the term of
5973 the form \verb|f(a)| connects strongest.
5974 \item When an operator is mixfix,
5975 \begin{itemize}
5976 \item If the operator symbol does not start or end with ``\verb|_|'',
5977 that is,
5978 no argument appears outside the term construct, the precedence is zero.
5979 An example is a singleton operator ``\verb|{_}|'';
5980 \item if it is a prefix unary operator, the precedence is 15.
5981 The unary ``\verb|-_|'' offers an example;
5982 \item Otherwise, the precedence is 41.
5983 \end{itemize}
5984 \end{itemize}
5985 Let us show examples.
5986 \begin{vvtm}
5987 \begin{ccode}
5988 CafeOBJ> module PREC-TEST {
5989 [ S ]
5990 op _+_ : S S -> S { prec: 33 }
5991 op _*_ : S S -> S { prec: 31 }
5992 op -_ : S -> S
5993 }
5994
5995 -- defining module PREC-TEST...._* done.
5996 CafeOBJ> select PREC-TEST
5997
5998 PREC-TEST> parse X:S + Y:S * Z:S .
5999
6000 (X + (Y * Z)) : S
6001 PREC-TEST> parse - X:S + Y:S .
6002
6003 ((- X) + Y) : S
6004 PREC-TEST>
6005 \end{ccode}
6006 \end{vvtm}
6007 According to the default rules, ``\verb|-_|'' has precedence 15,
6008 so connects stronger than ``\verb|_+_|''. Hence the result of the
6009 second parse.
6010
6011 \begin{warning}
6012 The current implementation accepts precedences between 0 and
6013 127 (inclusive). Out-of-bound precedences cause warnings,
6014 and are ignored.
6015 \end{warning}
6016
6017 \subsection{Left/Right Associativity}
6018
6019 \verb|l-assoc| states that the operator, which must be binary,
6020 associates to the left, and \verb|r-assoc| to the right. A
6021 typical usage is as follows. Under the declaration
6022 \begin{vvtm}
6023 \begin{ccode}
6024 op _+_ : S S -> S
6025 \end{ccode}
6026 \end{vvtm}
6027 ``\verb|X:S + X + X|'' has two parses
6028 \begin{vvtm}
6029 \begin{ccode}
6030 (X + X) + X
6031 X + (X + X)
6032 \end{ccode}
6033 \end{vvtm}
6034 If you declare\index{\texttt{l-assoc}}
6035 \begin{vvtm}
6036 \begin{ccode}
6037 op _+_ : S S -> S { l-assoc }
6038 \end{ccode}
6039 \end{vvtm}
6040 the parse is uniquely determined as ``\verb|(X + X) + X|'', while
6041 \index{\texttt{r-assoc}}
6042 \begin{vvtm}
6043 \begin{ccode}
6044 op _+_ : S S -> S { r-assoc }
6045 \end{ccode}
6046 \end{vvtm}
6047 dictates the parse to be ``\verb|X + (X + X)|''.
6048
6049 \begin{warning}
6050 When equational-theoretic associativity \verb|assoc|\index{\texttt{assoc}}
6051 is declared,
6052 the system has to decide how to decide connectivity. In that case,
6053 ``\verb|X + (X + X)|'' is equal to ``\verb|(X + X) + X|'' anyway, but
6054 it is necessary to decide on one or the other,
6055 to store and manipulate terms. In the current implementation,
6056 the system assumes right associativity.
6057 \end{warning}
6058
6059 Left/right associativity is allowed only on operators whose
6060 arities are both supersorts of the coarity.
6061
6062 \begin{warning}
6063 Note that the conditions for \verb|assoc| and for \verb|l-assoc|,
6064 \verb|r-assoc| differ. \verb|l-assoc| or \verb|r-assoc| is a
6065 parsing instruction. There is no inherent reason to
6066 prohibit these attributes, as long as you feel like it.
6067 In fact, the above condition only states that, if it does not
6068 hold, the attribute is almost meaningless.
6069 \end{warning}
6070
6071 \section{Constructor Attribute}\label{sec:p2-constructor-attribute}
6072
6073 The attribute \texttt{constr}\index{\texttt{constr}}\index{constructor (attribute)}
6074 states that the operator is a constructor of
6075 the coarity sort. For example,
6076 \begin{vvtm}
6077 \begin{ccode}
6078 op nil : -> List { constr }
6079 op __ : List List -> List { constr }
6080 \end{ccode}
6081 \end{vvtm}
6082 states that \verb|nil| and \verb|__| are constructors of \verb|List|.
6083 The idea of constructors is very important for many purposes, especially
6084 for theorem proving. The current system, however, does not take advantage of
6085 this concept in any way. To the system, \verb|constr| is a comment.
6086
6087 \section{Evaluation Strategy}\label{sec:p2-rew-strategy}
6088
6089 \cafeobj~allows the user to specify evaluation strategies of terms.
6090 In general, evaluation is nondeterministic. In selecting which rule to
6091 apply to which subterm, there is a certain license. And the selection
6092 affects performance. It may even decide whether the evaluation terminates.
6093
6094 \subsection{E-Strategy Attribute}
6095
6096 Many recent functional programming languages are using lazy evaluation
6097 strategies. There are several arguments for this trend, but unfettered
6098 laziness
6099 is not recommended, like in everyday life. One way to avoid its harmful
6100 effects is to incorporate in compilers a mechanism to intermingle eager
6101 evaluation strategies, so long as it does not change the results of
6102 computation. To see if it affects the results, strictness analyses are
6103 employed.
6104
6105 In \cafeobj, you need not be thoroughly lazy or eager. It allows you to
6106 assign different strategies to different operators. This means that
6107 you can have the best of both worlds, or eat and have a cake at once.
6108 Moreover, the system computes
6109 default strategies case by case, based on a rapid strictness analysis.
6110
6111 In accordance to a certain folklore, we sometimes say
6112 {\em E-strategy}\index{E-strategy} for evaluation
6113 strategy\index{evaluation strategy}. E-strategies are operator
6114 attributes, and are written in this form:
6115
6116 \begin{bsyntax} evaluation strategy \index{\texttt{strat:}} \Hline
6117 \texttt{strat:} ``\texttt{(}'' \textit{integer\_list} ``\texttt{)}''
6118 \end{bsyntax}
6119
6120 An integer list is a list of integers separated by blanks.
6121 Each integer in the list represents an argument. 1 is
6122 the first argument, 2 is the second, and so on, and 0 is
6123 the whole term.
6124
6125 E-strategies determine the order of evaluation.
6126 As an informative example, a built-in conditional is
6127 declared as follows.
6128 \begin{vvtm}
6129 \begin{ccode}
6130 op if_then_else_fi : Bool Int Int -> Int { strat: (1 0) }
6131 \end{ccode}
6132 \end{vvtm}
6133 This E-stragety ordains that the first argument (conditional part) be
6134 evaluated first, then the whole argument. Notice that the second
6135 and third arguments (\verb|then| and \verb|else| parts) do not appear in
6136 the list. These arguments are not evaluated, unless the whole term is
6137 evaluated and they are no longer arguments. Depending on the result of
6138 evaluating the first argument, one of these arguments is entirely
6139 irrelevant to the final result, so this strategy is clearly reasonable.
6140
6141 Another example is an eager strategy.
6142 \begin{vvtm}
6143 \begin{ccode}
6144 op _+_ : Int Int -> Int { strat: (1 2 0) }
6145 \end{ccode}
6146 \end{vvtm}
6147 The E-strategy requires the two arguments be evaluated before the whole
6148 term.
6149
6150 \subsection{Default Strategy}
6151
6152 If no explicit E-strategy is given, the default strategy is computed
6153 as follows. For each argument,
6154 \begin{quote}
6155 Evaluate the argument before the whole term,
6156 if there is a rewrite rule that defines the operator such that,
6157 in the place of the argument, a non-variable term appears.
6158 \end{quote}
6159 For example, given a binary operator \verb|f| and suitable variables
6160 \verb|X| and \verb|Y|, in the place of the first argument is
6161 a variable in
6162 \begin{vvtm}
6163 \begin{ccode}
6164 eq f(X,Y) = h(X) .
6165 \end{ccode}
6166 \end{vvtm}
6167 while it does not in
6168 \begin{vvtm}
6169 \begin{ccode}
6170 eq f(g(X),Y) = h(Y) .
6171 \end{ccode}
6172 \end{vvtm}
6173 If all the rewrite rules are of the former type, the first argument
6174 need not be evaluated immediately. Since any term (of a relevant sort)
6175 matches a variable, matching possibilities are unaffected. Furthermore,
6176 if the only rewrite rule defining \verb|f| is
6177 \begin{vvtm}
6178 \begin{ccode}
6179 eq f(X,Y) = h(Y) .
6180 \end{ccode}
6181 \end{vvtm}
6182 you are well advised to evaluate it later (or never):
6183 the righthand side does not contain \verb|X|, so evaluating it is futile.
6184
6185 On the other hand, if there are rules that contain non-variable terms in
6186 the argument place, the result of evaluating the argument affects
6187 matchability, so it is safe to evaluate it first.
6188
6189 \begin{warning}
6190 To be on the safe side, the default strategy always ends with
6191 \verb|0|. Former versions added last zeros even for
6192 user-supplied stategies. We concluded that this practice
6193 reminded us of overprotective nannies, and abandoned it.
6194 \end{warning}
6195
6196 If an operator definition is recursive, \cafeobj~may ignore an explicit
6197 E-strategy, and apply recursive rules successively. This is a kind of
6198 optimisation. To avoid this behaviour, give an E-strategy that does not
6199 have \verb|0| at the head of the list.
6200
6201 \subsection{Lazy Evaluation}
6202
6203 \cafeobj~provides two ways to specify lazy evaluation.
6204 \begin{itemize}
6205 \item[a.] Omit the arguments to be evaluated later, or
6206 \item[b.] Put the corresponding negative integers.
6207 \end{itemize}
6208 Delayed \verb|cons| is thus declared as either
6209 \begin{vvtm}
6210 \begin{ccode}
6211 op cons : Sexp Sexp -> Sexp { strat: (0) } -- (a)
6212 \end{ccode}
6213 \end{vvtm}
6214 or
6215 \begin{vvtm}
6216 \begin{ccode}
6217 op cons : Sexp Sexp -> Sexpt { strat: (-1 -2) } -- (b)
6218 \end{ccode}
6219 \end{vvtm}
6220 With the former declaration, both arguments are not evaluated unless
6221 they get outside of \verb|cons|. For example, suppose
6222 \verb|car| is defined as
6223 \begin{vvtm}
6224 \begin{ccode}
6225 op car : Sexp -> Sexp { strat: (1 0) }
6226 vars X Y : Sexp
6227 eq car(cons(X,Y)) = X . -- *
6228 \end{ccode}
6229 \end{vvtm}
6230 and you try to reduce the term
6231 \begin{vvtm}
6232 \begin{ccode}
6233 car(cons(1 + 0, nil))
6234 \end{ccode}
6235 \end{vvtm}
6236 where \verb|nil| is the usual constant. According to the given E-strategies,
6237 \begin{itemize}
6238 \item[1.] The argument of \verb|car| is to be evaluated first.
6239 \item[2.] But the argument is dominated by \verb|cons|, whose
6240 arguments are not to be evaluated, since \verb|1|, \verb|2| are
6241 not in the E-strategy. So ``\verb|cons(1 + 0, nil)|'' is
6242 returned as it is.
6243 \item[3.] The evaluation for \verb|car| continues, and the next
6244 argument in the E-strategy --- in fact \verb|0|, the whole term ---
6245 is to be evaluated.
6246 \item[4.] Using the equation marked \verb|*|, the whole term is
6247 rewritten to ``\verb|1 + 0|''.
6248 \item[5.] Evaluation continues with this term.
6249 \end{itemize}
6250
6251 With the declaration \verb|(b)|, both arguments are not evaluated until
6252 so forced. Evaluation is forced, when the argument is involved in
6253 matching. For example, to see the applicability of the equation (rewrite
6254 rule)
6255 \begin{vvtm}
6256 \begin{ccode}
6257 eq cadr(cons(X,cons(Y,Z))) = Y .
6258 \end{ccode}
6259 \end{vvtm}
6260 it is necessary to know whether the second argument of the outer \verb|cons|
6261 is dominated by \verb|cons|. Suppose a term has been rewritten so
6262 far to
6263 \begin{vvtm}
6264 \begin{ccode}
6265 cadr(cons(1,cdr(cons(2,cons(3,nil)))))
6266 \end{ccode}
6267 \end{vvtm}
6268 This term does not match the lhs as it is, because the second argument of
6269 the outmost \verb|cons| is dominated by \verb|cdr|, not by \verb|cons|.
6270 It does match, however, if the second argument is reduced (with the
6271 usual rule for \verb|cdr|) to \verb|cons(3,nil)|. \verb|-2| in the
6272 strategy states
6273 that the second argument is to be evaluated just at such moments.
6274
6275 Negative integers cannot be used if the operator is declared
6276 associative or commutative. In such cases, reduction ``on demand'', as
6277 explained above, is nigh impossible to implement.
6278
6279 %\begin{warning}
6280 % The lazy mechanism with negative integers no longer works.
6281 %\end{warning}
6282
6283 \subsection{Interpreting E-Strategies}
6284
6285 To understand the exact meaning of E-strategies, it is instructive to
6286 know the overall algorithm of evaluation. It is shown below.
6287 Remember that an E-strategy is a list of integers.
6288
6289 Given a term $t$,
6290 \begin{itemize}
6291 \item[]1. Let $l$ be the E-strategy of the dominant operator of $t$.
6292 \item[]2. If $l$ is empty, exit with $t$ as result. Otherwise, let
6293 $n$ be the first element of $l$.
6294 \begin{itemize}
6295 \item[]2-1. If $n = 0$, search a rewrite rule applicable to the whole
6296 $t$. If one such is found, replace $t$ with the substitution
6297 instance $t'$, and go back to 1.
6298 \item[]2-2. If $n > 0$, call this procedure recursively with the $n$-th
6299 argument of $t$ as input. Replace the argument with the result, and
6300 proceed to 3.
6301 \item[]2-3. If $n < 0$, mark the $-n$-th argument of $t$ as ``on demand'',
6302 and proceed to 3.
6303 \end{itemize}
6304 \item[]3. Remove the first element from $l$ and go back to 2.
6305 \end{itemize}
6306 Note that, after the step 2-1., the control is back to 1., and the rest
6307 of the current strategy is discarded.
6308 Note also that, when a negative integer appears,
6309 the corresponding argument is just marked and nothing else is done.
6310 The mark reveals its effects only during matching.
6311
6312 \section{Memo}\label{sec:p2-memo-attribute}
6313
6314 To evaluate similar terms repetitively is tedious to humans,
6315 but acceptable to the system. To evaluate {\em the same} terms repetitively,
6316 however, is sometimes too much, even to the system. It silently
6317 complains, by taking long time in responding. For the sake of
6318 efficiency, it helps if you state in advance that such and such
6319 evaluations are likely to occur quite often, and that remembering the
6320 previous results is a good idea.
6321
6322 Hence an attribute, called {\em memo}.
6323 This operator attribute tells the
6324 system to remember the results of evaluation where the operator appeared.
6325 More specifically, if you declare\index{\texttt{memo}}
6326 \begin{vvtm}
6327 \begin{ccode}
6328 op f**k : S S S -> S { memo }
6329 \end{ccode}
6330 \end{vvtm}
6331 and evaluate a term containing \verb|f**k(t1,t2,t3)|, the system
6332 take note of (1) \verb|f**k(t1,t2,t3)| itself, (2) \verb|f**k(r1,r2,r3)|,
6333 where \verb|ri|'s are the results of evaluating \verb|ti|'s, and
6334 (3) the result of evaluating (1). If the need arises to evaluate (1)
6335 again, the system gets the result (3) immediately. This mechanism
6336 not only speeds up the computation, it also saves memory space, when you
6337 evaluate terms that contain common subterms.
6338
6339 The memo attribute can only specified on functions, i.e. operators
6340 not defined by rules (See \label{sec:p2-rule-declaration} for rules).
6341
6342 The system does or does not remember the evaluation results
6343 after each evaluation. It is controlled by a switch, and
6344 \index{\texttt{clean memo}}
6345 \begin{vvtm}
6346 \begin{ccode}
6347 CafeOBJ> set clean memo on
6348 \end{ccode}
6349 \end{vvtm}
6350 tell the system to be forgetful. The default is \verb|off|.
6351 It is also possible to initialise memo storages by the following command.
6352 \begin{vvtm}
6353 \begin{ccode}
6354 CafeOBJ> clean memo
6355 \end{ccode}
6356 \end{vvtm}
6357
6358 The memo attributes may be ignored if you turn a switch off,\index{\texttt{memo}}
6359 as
6360 \begin{vvtm}
6361 \begin{ccode}
6362 CafeOBJ> set memo off
6363 \end{ccode}
6364 \end{vvtm}
6365 %To see the virtue of remembering, let us define a factorial function on
6366 %our own \verb|Nat|, and modify \verb|SIMPLE-NAT| and \verb|MULT| to
6367 %have operators with memos, as
6368 %\begin{vvtm}
6369 %\begin{ccode}
6370 % op _+_ : Nat Nat -> Nat { memo }
6371 % op _*_ : Nat NAt -> Nat { memo }
6372 %\end{ccode}
6373 %\end{vvtm}
6374 %Then
6375 %\begin{vvtm}
6376 %\begin{ccode}
6377 % CafeOBJ> module FACT {
6378 % protecting (MUTL)
6379 % op _! : Nat -> Nat { memo }
6380 % eq 0 ! = s(0) .
6381 % eq s(N:Nat) ! = (N !) * s(N).
6382 % }
6383 %
6384 % -- defining module FACT.._..* done.
6385 % CafeOBJ> set clean memo on
6386 %
6387 % CafeOBJ> set memo off
6388 %
6389 % CafeOBJ> reduce in FACT : s(s(s(s(s(s(0)))))) ! .
6390 % -- reduce in FACT : s(s(s(s(s(s(0)))))) !
6391 % ...The result is the large term, and is omitted...
6392 % (0.000 sec for parse, 1194 rewrites(0.060 sec), 2227 matches)
6393 % CafeOBJ> set memo on
6394 %
6395 % CafeOBJ> reduce in FACT : s(s(s(s(s(s(0)))))) ! .
6396 %
6397 % -- reduce in FACT : s(s(s(s(s(s(0)))))) !
6398 %
6399 % ...The result is the same large term, and is omitted...
6400 % (0.000 sec for parse, 1194 rewrites(0.060 sec), 2227 matches)
6401 % CafeOBJ>
6402 %\end{ccode}
6403 %\end{vvtm}
6404
6405 \section{Coherence} \label{sec:p2-coherence}
6406
6407 As explained in Section \ref{sec:p2-evaluation-command} and
6408 \ref{sec:p2-eval-exs-3},
6409 in evaluating a term, application of behavioural axioms is prohibited
6410 unless the redex is enclosed in an observation, and
6411 this restriction is to ensure the soundness of evaluation.
6412 This implies that the system can ignore the restriction
6413 so long as the application does not lead to an incorrect reasoning.
6414
6415 An attribute called \verb|coherent| may be used to tell the system
6416 to loosen the guard. It asserts that the operator acts
6417 indistinguishably on behaviourally equivalent terms, so that
6418 it is in effect a behavioural operator.
6419 For example, the declarations
6420 \begin{vvtm}
6421 \begin{ccode}
6422 [ T ]
6423 *[ S ]*
6424 ops a b : -> S
6425 bop g : S -> T
6426 op f : S -> T { coherent }
6427 beq [ a-sim-b ] a = b .
6428 \end{ccode}
6429 \end{vvtm}
6430 allow the system to apply \verb|a-sim-b| to \verb|f(a)|, where \verb|a|
6431 is not guarded by an observation, as well as to \verb|g(a)|, where
6432 \verb|a| is.
6433
6434 \section{Operator Attributes and Evaluations}\label{sec:p2-op-attr-eval}
6435
6436 Let us see how various attributes affect evaluation. In the case of
6437 an associative operator with identity,\label{exs:az-op}
6438 \begin{vvtm}
6439 \begin{ccode}
6440 module AZ-OP {
6441 [ S ]
6442 ops : a b c phi : -> S
6443 op __ : S S -> S { associative id: phi }
6444 }
6445
6446 \end{ccode}
6447 \end{vvtm}
6448 you may compute as follows.
6449 \begin{vvtm}
6450 \begin{ccode}
6451 phi a phi b phi c ->
6452 a phi b phi c ->
6453 a b phi c ->
6454 a b c
6455 \end{ccode}
6456 \end{vvtm}
6457 where \verb|phi|'s are eliminated wherever they appear.
6458 Indeed the system computes exactly like you.
6459 \begin{vvtm}
6460 \begin{ccode}
6461 CafeOBJ> select AZ-OP
6462
6463 AZ-OP> set trace on
6464
6465 AZ-OP> reduce phi a phi b phi c .
6466
6467 -- reduce in AZ-OP : phi a phi b phi c
6468 1>[1] rule: eq [ident12] : phi X-ID:S = X-ID:S
6469 { X-ID:S |-> a phi b phi c }
6470 1<[1] phi a phi b phi c --> a phi b phi c
6471 1>[2] rule: eq A2 phi X-ID:S A1 = A2 X-ID:S A1
6472 { A1 |-> phi c, X-ID:S |-> b, A2 |-> a }
6473 1<[2] a phi b phi c --> a b phi c
6474 1>[3] rule: eq A1 phi X-ID:S = A1 X-ID:S
6475 { X-ID:S |-> c, A1 |-> a b }
6476 1<[3] a b phi c --> a b c
6477 a b c : S
6478 (0.000 sec for parse, 3 rewrites(0.260 sec), 13 matches)
6479 AZ-OP>
6480 \end{ccode}
6481 \end{vvtm}
6482 The equations in this trace were generated by the system, to cope with
6483 identity. The exact mechanism need not be your
6484 concern, but beware that these extra equations may cause havoc
6485 (Section \ref{sec:p2-op-attr-limit}).
6486
6487 If you inspect the statistics, it is apparent that the numbers of
6488 matching attempts greatly exceeded those of rewrites. This is a general
6489 fact about evaluation modulo equational theory.
6490
6491 See what happens if the operator is also commutative, as in
6492 \label{exs:acz-op}
6493 \begin{vvtm}
6494 \begin{ccode}
6495 AZ-OP> module ACZ-OP {
6496 [ S ]
6497 ops : a b c phi : -> S
6498 op __ : S S -> S { associative commutative id: phi }
6499 }
6500
6501 -- defining module ACZ-OP....._._* done.
6502 AZ-OP> select ACZ-OP
6503
6504 ACZ-OP> reduce phi a phi b phi c .
6505
6506 -- reduce in ACZ-OP : phi a phi b phi c
6507 1>[1] rule: eq [ident14] : phi X-ID:S = X-ID:S
6508 { X-ID:S |-> a b phi phi c }
6509 1<[1] phi a phi b phi c --> a b phi phi c
6510 1>[2] rule: eq [ident14] : phi X-ID:S = X-ID:S
6511 { X-ID:S |-> a b phi c }
6512 1<[2] a b phi phi c --> a b phi c
6513 1>[3] rule: eq [ident14] : phi X-ID:S = X-ID:S
6514 { X-ID:S |-> a b c }
6515 1<[3] a b phi c --> a b c
6516 a b c : S
6517 (0.010 sec for parse, 3 rewrites(0.010 sec), 5 matches)
6518 ACZ-OP>
6519 \end{ccode}
6520 \end{vvtm}
6521 Again \verb|phi|'s were eliminated, but different rewrite rules
6522 were used, and, relying on commutativity, the system changed
6523 the order of listing at the first step.
6524
6525 Adding idempotency, we get\label{exs:aciz-op}
6526 \begin{vvtm}
6527 \begin{ccode}
6528 ACI-OP> module ACIZ-OP {
6529 [ S ]
6530 ops a b c phi : -> S
6531 op __ : S S -> S { assoc comm idem id: phi }
6532 }
6533
6534 -- defining module ACIZ....._._* done.
6535 ACI-OP> reduce in ACIZ-OP : phi a phi b phi c .
6536
6537 -- reduce in ACIZ-OP : phi a phi b phi c
6538 1>[1] rule: eq AC U-idem:S U-idem:S = AC U-idem:S
6539 { AC |-> phi a b c, U-idem:S |-> phi }
6540 1<[1] phi a phi b phi c --> phi a b c
6541 1>[2] rule: eq [ident16] : phi X-ID:S = X-ID:S
6542 { X-ID:S |-> a b c }
6543 1<[2] phi a b c --> a b c
6544 a b c : S
6545 (0.000 sec for parse, 2 rewrites(0.000 sec), 9 matches)
6546 ACI-OP>
6547 \end{ccode}
6548 \end{vvtm}
6549 \verb|phi|'s were stripped, but the reason for the first step
6550 (when two \verb|phi|'s were eliminated at once) was not that
6551 \verb|phi| was the identity, but that ``\verb|__|'' was
6552 idempotent.
6553
6554 \section{Further Example: Propositional Calculus}
6555 \label{sec:p2-propositional-calc}
6556
6557 The next example is a decision procedure of propositional logic.
6558 In this example, rewriting modulo associativity and commutativity
6559 is decisive.
6560
6561 \cafeobj~has a built-in module \verb|PROPC|, which is declared as follows.
6562 \label{exs:propc}
6563 \begin{vvtm}
6564 \begin{ccode}
6565 CafeOBJ> show PROPC
6566 sys:module! PROPC
6567 principal-sort Prop
6568 {
6569 imports {
6570 protecting (TRUTH)
6571 }
6572 signature {
6573 [ Prop, Identifier Bool < Prop ]
6574 op _ and _ : Prop Prop -> Prop { assoc comm constr prec: 55 r-assoc }
6575 op _ xor _ : Prop Prop -> Prop { assoc comm constr prec: 57 r-assoc }
6576 op _ or _ : Prop Prop -> Prop { assoc comm prec: 59 r-assoc }
6577 op not _ : Prop -> Prop { strat: (0 1) prec: 53 }
6578 op _ -> _ : Prop Prop -> Prop { strat: (0 1 2) prec: 61 }
6579 op _ <-> _ : Prop Prop -> Prop { assoc prec: 63 r-assoc }
6580 }
6581 axioms {
6582 var p : Prop
6583 var q : Prop
6584 var r : Prop
6585 eq p and false = false .
6586 eq p and true = p .
6587 eq p and p = p .
6588 eq p xor false = p .
6589 eq p xor p = false .
6590 eq p and (q xor r) = p and q xor p and r .
6591 eq p or q = p and q xor p xor q .
6592 eq not p = p xor true .
6593 eq p -> q = p and q xor p xor true .
6594 eq p <-> q = p xor q xor true .
6595 }
6596 }
6597
6598 CafeOBJ>
6599 \end{ccode}
6600 \end{vvtm}
6601 ``\verb|sys:module!|'' is an introductory keyword for built-in modules.
6602 \begin{itemize}
6603 \item An atomic proposition is an element of the built-in
6604 sort \verb|Identifier|, declared in \newline
6605 \verb|CHAOS:IDENTIFIER|, or of the sort \verb|Bool|,
6606 declared in \verb|TRUTH|.
6607 \verb|Identifier| contains character strings.
6608 \verb|Bool| contains \verb|true| and \verb|false|.
6609 \item Propositional connectives are \texttt{and}, \texttt{or}, \texttt{->},
6610 \texttt{not}, \texttt{<->}, and \texttt{xor} (exclusive or).
6611 \item An irreducible term is constructed out of \texttt{and}, \texttt{xor},
6612 \texttt{true}, \texttt{false}, and identifiers.
6613 It is in disjunctive normal form (by regarding \texttt{xor} as disjunction).
6614 In particular, tautologies are reducible to \texttt{true}.
6615 \item The precedences of connectives are ordered as
6616 \texttt{not}, \texttt{and}, \texttt{xor}, \texttt{or}, \texttt{->}, \texttt{<->}.
6617 \end{itemize}
6618 Evaluations in this module proceed as follows. Firstly,
6619 \begin{vvtm}
6620 \begin{ccode}
6621 a -> b <-> not b -> not a
6622 \end{ccode}
6623 \end{vvtm}
6624 states $a \to b \leftrightarrow \neg b \to \neg a$, a tautology
6625 (equivalence of contrapositives).
6626 \begin{vvtm}
6627 \begin{ccode}
6628 CafeOBJ> select PROPC
6629
6630 PROPC> set trace on
6631
6632 PROPC> reduce a -> b <-> not b -> not a .
6633
6634 -- reduce in PROPC : a -> b <-> not b -> not a
6635 1>[1] rule: eq p:Prop -> q:Prop = p:Prop and q:Prop xor p:Prop xor
6636 true
6637 { p:Prop |-> a, q:Prop |-> b }
6638 1<[1] a -> b --> a and b xor a xor true
6639 1>[2] rule: eq p:Prop -> q:Prop = p:Prop and q:Prop xor p:Prop xor
6640 true
6641 { p:Prop |-> not b, q:Prop |-> not a }
6642 1<[2] not b -> not a --> not b and not a xor not b xor true
6643 1>[3] rule: eq not p:Prop = p:Prop xor true
6644 { p:Prop |-> b }
6645 1<[3] not b --> b xor true
6646 1>[4] rule: eq not p:Prop = p:Prop xor true
6647 { p:Prop |-> a }
6648 1<[4] not a --> a xor true
6649 1>[5] rule: eq p:Prop and (q:Prop xor r:Prop) = p:Prop and q:Prop
6650 xor p:Prop and r:Prop
6651 { p:Prop |-> b xor true, q:Prop |-> true, r:Prop |-> a }
6652 1<[5] (b xor true) and (a xor true) --> (b xor true) and true xor
6653 (b xor true) and a
6654 1>[6] rule: eq p:Prop and true = p:Prop
6655 { p:Prop |-> b xor true }
6656 1<[6] (b xor true) and true --> b xor true
6657 1>[7] rule: eq p:Prop and (q:Prop xor r:Prop) = p:Prop and q:Prop
6658 xor p:Prop and r:Prop
6659 { p:Prop |-> a, q:Prop |-> true, r:Prop |-> b }
6660 1<[7] (b xor true) and a --> a and true xor a and b
6661 1>[8] rule: eq p:Prop and true = p:Prop
6662 { p:Prop |-> a }
6663 1<[8] a and true --> a
6664 1>[9] rule: eq AC xor p:Prop xor p:Prop = AC xor false
6665 { AC |-> true xor a xor a and b, p:Prop |-> true xor b }
6666 1<[9] b xor true xor a xor a and b xor b xor true xor true --> true
6667 xor a xor a and b xor false
6668 1>[10] rule: eq p:Prop xor false = p:Prop
6669 { p:Prop |-> a and b xor true xor a }
6670 1<[10] true xor a xor a and b xor false --> a and b xor true xor a
6671 \end{ccode}
6672 \end{vvtm}
6673 \begin{vvtm}
6674 \begin{ccode}
6675 1>[11] rule: eq p:Prop <-> q:Prop = p:Prop xor q:Prop xor true
6676 { p:Prop |-> a and b xor a xor true, q:Prop |-> a and b xor true
6677 xor a }
6678 1<[11] a and b xor a xor true <-> a and b xor true xor a --> a and
6679 b xor a xor true xor a and b xor true xor a xor true
6680 1>[12] rule: eq AC xor p:Prop xor p:Prop = AC xor false
6681 { AC |-> true, p:Prop |-> true xor a and b xor a }
6682 1<[12] a and b xor a xor true xor a and b xor true xor a xor true
6683 --> true xor false
6684 1>[13] rule: eq p:Prop xor false = p:Prop
6685 { p:Prop |-> true }
6686 1<[13] true xor false --> true
6687 true : Bool
6688 (0.000 sec for parse, 13 rewrites(0.140 sec), 75 matches)
6689 PROPC>
6690 \end{ccode}
6691 \end{vvtm}
6692 Observe that associativity and commutativity are frequently used.
6693 To trace everything makes the exposition rather long, so we omit
6694 traces in the sequel.
6695 \begin{vvtm}
6696 \begin{ccode}
6697 PROPC> set trace off
6698 PROPC> reduce not(a or b) <-> not a and not b .
6699 -- reduce in PROPC : not (a or b) <-> not a and not b
6700 true : Bool
6701 (0.000 sec for parse, 11 rewrites(0.020 sec), 70 matches)
6702 PROPC> reduce c or c and d <-> c .
6703 -- reduce in PROPC : c or c and d <-> c
6704 true : Bool
6705 (0.000 sec for parse, 7 rewrites(0.190 sec), 34 matches)
6706 PROPC> reduce a <-> not c .
6707 -- reduce in PROPC : a <-> not c
6708 a xor c : Prop
6709 (0.000 sec for parse, 4 rewrites(0.010 sec), 15 matches)
6710 PROPC> reduce a and b xor c xor b and a .
6711 -- reduce in PROPC : a and b xor c xor b and a
6712 c : Identifier
6713 (0.010 sec for parse, 2 rewrites(0.000 sec), 21 matches)
6714 PROPC> reduce a <-> a <-> a <-> a .
6715 -- reduce in PROPC : a <-> a <-> a <-> a
6716 true : Bool
6717 (0.010 sec for parse, 9 rewrites(0.000 sec), 18 matches)
6718 PROPC> reduce a -> b and c <-> (a -> b) and (a -> c) .
6719 -- reduce in PROPC : a -> b and c <-> (a -> b) and (a -> c)
6720 true : Bool
6721 (0.010 sec for parse, 25 rewrites(0.050 sec), 184 matches)
6722 PROPC>
6723 \end{ccode}
6724 \end{vvtm}
6725 It is pleasing to see that the usual logical truths are proved
6726 to be true, by just evaluating the corresponding terms.
6727 Here the logical basis of the language is in stark relief.
6728 If you think of rewrite rules as deduction rules, a computation is
6729 a proof. The set of rewrite rules is sound iff all the statements
6730 reducible to \verb|true| are valid. It is complete iff
6731 all the valid statements are reducible to \verb|true|.
6732
6733 \section{Limitation of the Implementation}\label{sec:p2-op-attr-limit}
6734
6735 As a cautious conclusion to operator attributes, we must point to the
6736 major limitations of
6737 the current implementation. As mentioned earlier, the treatment of
6738 idempotency is inadequate. The other two problems concern the
6739 enormity of extra rewrite rules. You should have noticed that, to realise
6740 rewriting modulo identity or associativity, the system adds equations
6741 surreptitiously. Consider first the case of identities.
6742
6743 Suppose in \verb|AZ-OP| in Section \ref{exs:az-op}, there were another
6744 operator declaration
6745 \begin{vvtm}
6746 \begin{ccode}
6747 op first : S -> S
6748 \end{ccode}
6749 \end{vvtm}
6750 and an equation
6751 \begin{vvtm}
6752 \begin{ccode}
6753 eq first(X:S X':S) = X .
6754 \end{ccode}
6755 \end{vvtm}
6756 A special case of the equation is
6757 \begin{vvtm}
6758 \begin{ccode}
6759 eq first(X) = X .
6760 \end{ccode}
6761 \end{vvtm}
6762 where \verb|X'| is bound to \verb|phi| and the identity is cancelled.
6763 The completion procedure for implementing identities does add this
6764 equation, as well as
6765 \begin{vvtm}
6766 \begin{ccode}
6767 eq first(X') = phi .
6768 \end{ccode}
6769 \end{vvtm}
6770 which is a specialisation to the case when \verb|X| is \verb|phi|.
6771 Thus we get a specification where
6772 \begin{vvtm}
6773 \begin{ccode}
6774 phi = first(a) = a = first(a b) = phi = ...
6775 \end{ccode}
6776 \end{vvtm}
6777 holds. This result is somewhat unexpected. As a TRS, the two equations
6778 annihilate the Church-Rosser property.
6779
6780 A different problem arises if you
6781 modify the declaration of \verb|xor| in \verb|PROPC| (Section \ref{exs:propc})
6782 as
6783 \begin{vvtm}
6784 \begin{ccode}
6785 op _xor_ : Prop Prop -> Prop { assoc comm id: false }
6786 \end{ccode}
6787 \end{vvtm}
6788 i.e., adding the identity attribute.
6789 Then in the presence of the equation
6790 \begin{vvtm}
6791 \begin{ccode}
6792 eq p and (q xor r) = p and q xor p and r .
6793 \end{ccode}
6794 \end{vvtm}
6795 the completion procedure adds
6796 \begin{vvtm}
6797 \begin{ccode}
6798 eq p and r = (p and false) xor (p and r) .
6799 \end{ccode}
6800 \end{vvtm}
6801 which is a special case, where \verb|q| is replaced by \verb|false|.
6802 This equation is hazardous, since you now get an infinite
6803 rewrite sequence
6804 \begin{vvtm}
6805 \begin{ccode}
6806 a and b -->
6807 (a and false) xor (a and b) -->
6808 (a and false) xor (a and false) xor (a and b) -->
6809 :
6810 \end{ccode}
6811 \end{vvtm}
6812 Even if such a hazard is avoided, performance may be compromised,
6813 by hundreds of extra unwanted equations. If you encounter misbehaviours,
6814 then, ``\verb|id:|'' is a possible suspect. If it is the culprit,
6815 change to ``\verb|idr:|'',\index{\texttt{idr:}} which does not force completion.
6816
6817 As to associativity, a fateful combination with sort ordering may
6818 generate dozens of unnecessary equations. For example, in the built-in
6819 module \verb|INT|, \verb|Nat| is a subsort of \verb|Int|, and
6820 the addition ``\verb|_+_|'' is declared on both sorts. As a consequence,
6821 an equation
6822 \begin{vvtm}
6823 \begin{ccode}
6824 eq 0 + I = I .
6825 \end{ccode}
6826 \end{vvtm}
6827 induces an extra equation
6828 \begin{vvtm}
6829 \begin{ccode}
6830 eq 0 + I + ac_E = ac_E + I .
6831 \end{ccode}
6832 \end{vvtm}
6833 This equation has a lefthand side that is costly to match, yet adds
6834 nothing to the power of evaluation. To check the necessity of
6835 such extra equations is not easy. As of now, the system bets on
6836 the safer side, and adds equations nonchalantly.
6837
6838 \chapter{Module Structure}\label{sec:p2-module-hierarchies}
6839
6840 \section{Names and Contexts}
6841
6842 \subsection{Context and Current Module}\label{sec:p2-current-module}
6843
6844 Import declarations define a hierarchy of modules. As a directed graph,
6845 it is acyclic: a module cannot import itself, either directly or
6846 indirectly. The directed acyclic graph is called
6847 {\em context}\index{context (module)},
6848 and for a paricular module \verb|M| in the
6849 graph, the complete subgraph reachable from \verb|M| is called
6850 the context of \verb|M|. You may regard the context of \verb|M| as
6851 collections of sorts, operators, variables, and axioms, which can
6852 be referred to within \verb|M|.
6853
6854 The concept of current module\index{current module} is to establish
6855 a context in the above sense. Such commands as \verb|reduce| and
6856 \verb|parse| presuppose a context. The current module is set by the
6857 following command.
6858
6859 \begin{bsyntax} \texttt{select} command \index{\texttt{select}} \Hline
6860 \texttt{select} \textit{module\_name}
6861 \end{bsyntax}
6862
6863 You are always reminded of
6864 the current module by the prompt --- you already have seen many
6865 session examples.
6866
6867 You may make the context follow your every step, so that if you
6868 define a module or inspect its contents, the current module switches
6869 to that module, by the command\index{\texttt{set}}\index{\texttt{auto}}
6870 \index{\texttt{context}}
6871 \begin{vvtm}
6872 \begin{ccode}
6873 CafeOBJ> set auto context on
6874 \end{ccode}
6875 \end{vvtm}
6876 The default is \verb|off|.
6877 The effects of this switch are illustrated in the following session.
6878 \begin{vvtm}
6879 \begin{ccode}
6880 CafeOBJ> set auto context on
6881
6882 CafeOBJ> module M { [ S ] op e : -> S }
6883
6884 -- defining module M.._* done.
6885 M> module N { [ S ] }
6886
6887 -- defining module N._* done.
6888 N> show M
6889 module M {
6890 imports {
6891 protecting (BOOL)
6892 }
6893 signature {
6894 [ S ]
6895 op e : -> S
6896 }
6897 }
6898
6899 M> show N
6900 module N {
6901 imports {
6902 protecting (BOOL)
6903 }
6904 signature {
6905 [ S ]
6906 }
6907 }
6908
6909 N>
6910 \end{ccode}
6911 \end{vvtm}
6912 Notice that the prompt changed to \verb|M| (\verb|N|), each time
6913 \verb|M| (\verb|N|) was defined or inspected. The current module ---
6914 hence context --- can be shown as follows.\index{\texttt{context}}
6915 \begin{vvtm}
6916 \begin{ccode}
6917 N> show context
6918 -- current context :
6919 [module] N
6920 [special bindings]
6921 $$term = none.
6922 $$subterm = no subterm selection is made by `choose'.
6923 [bindings] empty.
6924 [selections] empty.
6925 [pending actions] none.
6926 [stop pattern] not specified.
6927
6928 N>
6929 \end{ccode}
6930 \end{vvtm}
6931 The output contains the states that are local to the current context.
6932 For example, under \verb|bindings| are listed context variables
6933 (Section \ref{sec:p2-let-command}). The others concern theorem
6934 proving tools, to be explained (Chapter \ref{sec:p2-theorem-proving}).
6935
6936 \verb|show| command is also used to print contexts. To print the context
6937 of \verb|M|, type
6938 \begin{vvtm}
6939 \begin{ccode}
6940 CafeOBJ> show module tree M
6941 \end{ccode}
6942 \end{vvtm}
6943
6944 A submodule of a module is identified by a name, so during a session
6945 it is possible for the definition of a submodule to
6946 change --- when you redefine that submodule, or when you unintentionally
6947 use the same name for a different module. In such a case, the system
6948 does not take any housekeeping exercise until necessary --- until,
6949 for example, an evaluation command is entered. You may make the
6950 system to be more diligent, by flicking a switch.
6951 \index{\texttt{auto}}\index{\texttt{reconstruct}}
6952 \begin{vvtm}
6953 \begin{ccode}
6954 CafeOBJ> set auto reconstruct on
6955 \end{ccode}
6956 \end{vvtm}
6957 When the switch is on, every time a module is redefined, every
6958 context surrounding it is forced to reflect the change at once.
6959
6960 If you are so inclined, you may change the prompt once and for all.
6961 \index{\texttt{prompt}}
6962 \begin{vvtm}
6963 \begin{ccode}
6964 CafeOBJ> prompt Haskell>
6965
6966 Haskell>
6967 \end{ccode}
6968 \end{vvtm}
6969 In the reset of the session, the prompt would not change until
6970 you invoke the command again\footnote{For an obvious reason, the above change
6971 does not force the system to accept Haskell.}.
6972
6973 \subsection{Module Sharing}\label{sec:p2-module-sharing}
6974
6975 It is possible that some modules are imported several times.
6976 For example, the declarations
6977 \begin{vvtm}
6978 \begin{ccode}
6979 module M0 { ... }
6980 module M00 { protecting (M0) ... }
6981 module M01 { protecting (M0) ... }
6982 module M001 { protecting (M00) protecting (M01) ... }
6983 \end{ccode}
6984 \end{vvtm}
6985 makes \verb|M001| import \verb|M0| twice. The basic
6986 convention is that, in such cases, \verb|M0|
6987 is regarded as imported only once, and its single copy is
6988 shared throughout. This convention works fine when a module is
6989 imported with \verb|protecting| mode, since a module then has
6990 essentially the same models everywhere. The problem arises
6991 when \verb|extending| or \verb|using| modes appear somewhere.
6992 In general, importation modes are determined as follows.
6993
6994 Let us write \verb|M -m-> M'| if \verb|M| directly imports
6995 \verb|M'| with importation mode \verb|m|. When \verb|M| (directly or
6996 indirectly) imports \verb|M'|, there are several chains
6997 \begin{vvtm}
6998 \begin{ccode}
6999 M -m1-> M1 -m2-> ... -mn-> Mn = M'
7000 \end{ccode}
7001 \end{vvtm}
7002 to \verb|M'|. Each such chain determines the mode with which
7003 \verb|M| imports \verb|M'|, as the weakest among all the
7004 \verb|mi|'s. For example, in the chain
7005 \begin{vvtm}
7006 \begin{ccode}
7007 M -pr-> M1 -ex-> M2 -pr-> M'
7008 \end{ccode}
7009 \end{vvtm}
7010 \verb|M'| is imported via \verb|ex| mode.
7011
7012 The mode with which \verb|M'| is imported is the weakest among
7013 all the chains along which \verb|M'| is imported.
7014
7015 \subsection{Qualifying Names}\label{sec:p2-naming-and-qualification}
7016
7017 In Section \ref{sec:p2-parsing-qualifier}, we have shown how to qualify
7018 terms by sort names. You can also qualify sort names and operator names,
7019 this time by module names.
7020
7021 \cafeobj~allows you to use the same name for different sorts.
7022 A typical case is to declare sorts in different but related
7023 modules: variants of stack definitions, for example, are likely to
7024 have a common sort name \verb|Stack|.
7025 So long as they do not appear in the same context, those
7026 sorts do not cause confusion. The problem arises when you import
7027 two modules that contain sorts of the same name.
7028 It is unlikely to import both modules directly, but it is
7029 definitely possible to import them indirectly. To avoid confusion
7030 you can qualify names as follows.
7031
7032 \begin{bsyntax} reference to sort/operator/parameters \Hline
7033 \textit{name}$[$``\texttt{.}''\textit{module\_name}$]$
7034 \end{bsyntax}
7035
7036 where the name is a sort name, an operator name, or a parameter name
7037 (Section \ref{sec:p2-parameter}). According to the above
7038 syntactical rule, the references that
7039 appeared so far, such as \verb|Nat|, could be qualified, such
7040 as ``\verb|Nat.SIMPLE-NAT|''.
7041 A module name may be a parameter name (Section \ref{sec:p2-parameter}).
7042
7043 To avoid ambiguity, if an operator name contains blanks or
7044 periods, it must be parenthesised, as ``\verb|(_._).M|''.
7045
7046 The referent of a qualified name is the one that is in the context
7047 of the qualifier. ``\verb|Nat.SIMPLE-NAT|'' refers to the sort
7048 \verb|Nat| in
7049 the context of \verb|SIMPLE-NAT|. Note that this does {\em not} imply that
7050 \verb|Nat| is declared in \verb|SIMPLE-NAT|: it only says that
7051 {\em somewhere} in the context of \verb|SIMPLE-NAT|, \verb|Nat| is declared.
7052 If, in the context of \verb|SIMPLE-NAT|, more than one \verb|Nat| are
7053 declared, the qualifier fails to identify which. In that case,
7054 you have to be more specific.
7055
7056 \begin{warning}
7057 The above qualification of operator names are for {\em referring} to
7058 operators in \verb|show| commands, mappings, etc.
7059 You cannot use qualification in writing a term.
7060 \end{warning}
7061
7062 \subsection{Referring to Operators}\label{sec:p2-operator-reference}
7063
7064 Operator names appear in renaming and view declarations
7065 (both are to be explained). In these constructs, a single name may
7066 refer to more than one operators, due to overloading.
7067 Special care needs to be taken, therefore, to see which operator
7068 names refer to which operators.
7069
7070 Note first that you have to distinguish operators with different
7071 number of arguments, as
7072 \begin{vvtm}
7073 \begin{ccode}
7074 op f : Nat -> Nat
7075 \end{ccode}
7076 \end{vvtm}
7077 and
7078 \begin{vvtm}
7079 \begin{ccode}
7080 op f : Nat Nat -> Nat
7081 \end{ccode}
7082 \end{vvtm}
7083 This problem does not arise if operators are mixfix, since their
7084 symbols contain placeholders ``\verb|_|''s. All in all, you have this
7085 definition.
7086
7087 \begin{bsyntax} operator name \Hline
7088 $\{$ \textit{mixfix\_operator\_symbol} $|$ \textit{standard\_operator\_symbol} $\}[$``\texttt{/}''\textit{number\_of\_arguments}$]$
7089 \end{bsyntax}
7090
7091 You should not insert blanks between the operator symbol and ``\verb|/|'', or
7092 between ``\verb|/|'' and the number of arguments. Using this form,
7093 the above two declarations may be distinguished as
7094 ``\verb|f/1|'' and ``\verb|f/2|''.
7095
7096 With this preparation, let us define in detail the relationship between
7097 operator names and their referents. In \cafeobj, an operator name
7098 refers to a group of operators, rather than a single operator.
7099 Given a module \verb|M|, consider, for example, an operator declaration
7100 \begin{vvtm}
7101 \begin{ccode}
7102 op f : Nat Nat -> Nat
7103 \end{ccode}
7104 \end{vvtm}
7105 in \verb|M|, and let $G$ be the group this operator belongs to.
7106 \begin{enumerate}
7107 \item[1.] Another operator with the same name (i.e. ``\verb|f/2|''), and whose
7108 arity is in the same connected component as \verb|Nat Nat|, belongs to
7109 $G$.
7110 \item[2.] Yet another operator, with the same name but with arity outside
7111 the connected component, belongs to $G$ only if it is declared in \verb|M|.
7112 \item[3.] No other operator belongs to $G$.
7113 \end{enumerate}
7114 Here the connected component of ``\verb|Nat Nat|'' is taken from
7115 the product graph.
7116 The operator name ``\verb|f/2|'' in the context of \verb|M| refers to
7117 the group $G$, which may contain several distinct operators, due to the
7118 above rules. Accordingly, the qualified name ``\verb|(f/2).M|'' refers to
7119 $G$.
7120
7121 Intuitively, qualification by \verb|M| states that the name is declared
7122 in \verb|M|. Hence 2. precludes operators declared in other modules.
7123 But what about 1.? Consider, for example, the code
7124 \begin{vvtm}
7125 \begin{ccode}
7126 module NAT {
7127 [ Nat ]
7128 ...
7129 op _+_ : Nat Nat -> Nat
7130 ...
7131 }
7132
7133 module INT {
7134 protecting (NAT)
7135 [ Nat < Int ]
7136 ...
7137 op _+_ : Int Int -> Int
7138 ...
7139 }
7140 \end{ccode}
7141 \end{vvtm}
7142 and the reference ``\verb|(_+_).INT|''. It certainly refers to ``\verb|_+_|''
7143 on \verb|Int|, but it should also refer to ``\verb|_+_|'' on \verb|Nat|, since
7144 \cafeobj~regards an overloaded operator on subsorts as the restriction.
7145 On \verb|Nat|, two ``\verb|_+_|''s must behave identically. Hence the
7146 rule 1.
7147
7148 %To probe the consequences of the above definition, let us introduce contrived
7149 %module declarations. Firstly,
7150 %\begin{vvtm}
7151 %\begin{ccode}
7152 % module M {
7153 % signature {
7154 % [ A < B, C D ]
7155 % op op1 : A A -> A
7156 % op op1 : B B -> B
7157 % op _+_ : C C -> C
7158 % op _+_ : D D -> D
7159 % op op1 : C C C -> C
7160 % op op2 : A A -> A
7161 % }
7162 % }
7163 %\end{ccode}
7164 %\end{vvtm}
7165 %Within \verb|M|, we only need to consider the rules 2. and 3., and get
7166 %the following operator groups.
7167 %\begin{itemize}
7168 %\item[] Group \verb|op1/2|: \verb|{ op1 : A A -> A, op1 : B B -> B }|
7169 %\item[] Group \verb|op1/3|: \verb|{ op1 : C C C -> C }|
7170 %\item[] Group \verb|_+_|: \verb|{ _+_ : C C -> C, _+_ : D D -> D }|
7171 %\item[] Group \verb|op2/2|: \verb|{ op2 : A A -> A }|
7172 %\end{itemize}
7173 %Secondly,
7174 %\begin{vvtm}
7175 %\begin{ccode}
7176 % module M' {
7177 % protecting(M)
7178 % signature {
7179 % [ D < E, F ]
7180 % op _+_ : E E -> E
7181 % op _+_ : F F -> F
7182 % op op1 : F F -> F
7183 % op op2 : E E -> E
7184 % }
7185 % }
7186 %\end{ccode}
7187 %\end{vvtm}
7188 %Operators are massively overloaded. To simplify the exposition, use
7189 %qualification by \verb|M| and expand the declarations:
7190 %\begin{vvtm}
7191 %\begin{ccode}
7192 % signature {
7193 % [ A.M < B.M, C.M, D.M < E, F ]
7194 % op op1.M : A.M A.M -> A.M
7195 % op op1.M : B.M B.M -> B.M
7196 % op _+_.M : C.M C.M -> C.M
7197 % op _+_.M : D.M D.M -> D.M
7198 % op op1.M : C.M C.M C.M -> C.M
7199 % op op2.M : A.M A.M -> A.M
7200 % op _+_ : E E -> E
7201 % op _+_ : F F -> F
7202 % op op1 : F F -> F
7203 % op op2 : E E -> E
7204 % }
7205 %\end{ccode}
7206 %\end{vvtm}
7207 %Using the rules 1., 2. and 3., we get the following group.
7208 %\begin{itemize}
7209 %\item[] Group \verb|op1/2|:
7210 % \verb|{ op1.M : A.M A.M -> A.M, op1.M : B.M B.M -> B.M }|
7211 %\item[] Group \verb|op1/2|: \verb|{ op1 : F F -> F }|
7212 %\item[] Group \verb|_+_|:
7213 % \verb|{ _+_ : E E -> E, _+_ : F F -> F, _+_.M : D.M D.M -> D.M }|
7214 %\item[] Group \verb|_+_|: \verb|{ _+_.M : C.M C.M -> C.M }|
7215 %\item[] Group \verb|op1/3|: \verb|{ op1.M : C.M C.M C.M -> C.M }|
7216 %\item[] Group \verb|op2/2|: \verb|{ op2 : E E -> E }|
7217 %\item[] Group \verb|op2/2|: \verb|{ op2.M : A.M A.M -> A.M }|
7218 %\end{itemize}
7219 %Note well how the groups are rearranged, and observe the appearance of
7220 %groups with the same names. We may avoid the name conflict over
7221 %``\verb|op1/2|'' or ``\verb|op2/2|'' by qualifying with \verb|M|, but
7222 %``\verb|_+_|'' would not succumb to this trick. ``\verb|(_+_).M|'' refers to
7223 %the group before rearrangement, not to one of the new groups.
7224 %\cafeobj~does not offer a general solution to this problem.
7225 %
7226 %\begin{warning}
7227 % This section will be entirely deleted from the future version.
7228 %\end{warning}
7229
7230 \section{Parameters and Views}
7231
7232 One of the salient features of \cafeobj~is a parameter mechanism.
7233 By putting aside certain sorts and operators as parameters,
7234 you can declare a generic module. A parameter unit is itself a module
7235 so that, on top of syntactical requirements, semantic constraints may
7236 be imposed on actual parameters. To instantiate a parameterised module is
7237 to replace the parameters with modules that satisfy the syntactic {\em and}
7238 semantic constraints.
7239
7240 \subsection{Parameterised Module}\label{sec:p2-parameter}
7241
7242 To declare a parameterised module, attach a parameter interface
7243 to the module declaration construct.
7244
7245 \begin{bsyntax} parameterised module \index{\texttt{module}} \Hline
7246 \texttt{module} \textit{module\_name} ``\texttt{(}'' \textit{list\_of\_parameters} ``\texttt{)}'' ``\texttt{\{}''
7247 ~~~~\textit{module\_element} $*$
7248 ``\texttt{\}}''
7249 \end{bsyntax}
7250
7251 A list of parameters is comma-separated. A {\em parameter}\index{parameter}
7252 has the following construct.
7253
7254 \begin{bsyntax} parameter \Hline
7255 $[$ \textit{importation\_mode} $]$ \textit{parameter\_name} ``\texttt{::}'' \textit{module\_name}
7256 \end{bsyntax}
7257
7258 A parameter name is an arbitrary character string, and is used to
7259 refer to the parameter module in the module body. The module name
7260 after ``\verb|::|'' describes requirements that must be satisfied by
7261 the actual parameters. The same module may act as distinct parameters,
7262 and parameter names distinguish them.
7263 For importation modes, which are optional, see
7264 Section \ref{sec:p2-params-as-subs}.
7265
7266 \begin{warning}
7267 In ancient times, parameters were enclosed with square brackets ``\verb|[|'',
7268 ``\verb|]|''.
7269 \end{warning}
7270
7271 \begin{warning}
7272 Do not forget to put blanks before and after ``\verb|::|''.
7273 \end{warning}
7274
7275 For example, the following modules define indexed lists,
7276 where both indices and data may come from arbitrary sets.
7277 \label{exs:ilist}
7278 \begin{vvtm}
7279 \begin{ccode}
7280 module* ONE { [ Elt ] }
7281
7282 module! ILIST ( IDX :: ONE, DAT :: ONE ) {
7283 [ Ilist ]
7284 [ Elt.DAT < ErrD ]
7285 op undef : -> ErrD
7286 op empty : -> Ilist
7287 op put : Elt.IDX Elt.DAT Ilist -> Ilist
7288 op _[_] : Ilist Elt.IDX -> ErrD
7289 -- -----------------------------
7290 vars I I' : Elt.IDX
7291 var D : Elt.DAT
7292 var L : Ilist
7293 eq put(I,D,L) [ I' ] = if I == I' then D else L [ I' ] fi .
7294 eq empty [ I ] = undef .
7295 }
7296 \end{ccode}
7297 \end{vvtm}
7298 Observe how the same module \verb|ONE| is used as different parameters.
7299 Inside \verb|ILIST|, sorts and operators of a parameter may be
7300 referred to with qualified names, like ``\verb|Elt.DAT|''
7301 (cf. Section \ref{sec:p2-naming-and-qualification}).
7302
7303 As an aside, the current version of \cafeobj~has a built-in module
7304 called \verb|TRIV|, which is the same as \verb|ONE| above.\label{exs:triv}
7305 Using this built-in, you may as well have written
7306 \begin{vvtm}
7307 \begin{ccode}
7308 module! ILIST ( IDX :: TRIV, DAT :: TRIV ) \{ ...
7309 \end{ccode}
7310 \end{vvtm}
7311 Further, error sorts, such as \verb|ErrD|, are implicitly declared
7312 (Section \ref{sec:p2-flexible-typing}), so you need not declare
7313 \verb|ErrD| as above.
7314
7315 \subsection{Pre-view (Not Preview)}\label{sec:p2-pre-view}
7316
7317 This section contains a preliminary to the following sections,
7318 and is rather technical. To bind actual parameter modules to formals,
7319 we need ways to relate two modules. Since modules denote structures,
7320 we rely on the idea of structure-preserving mappings. Such mappings
7321 are usually called homomorphisms.\index{homomorphism}
7322 Intuitively, in
7323 \begin{vvtm}
7324 \begin{ccode}
7325 module M { [ S ] }
7326 module M' { [ S' ] }
7327 module N { [ T U ] }
7328 \end{ccode}
7329 \end{vvtm}
7330 the modules \verb|M| and \verb|M'| are essentially the same, since
7331 the difference lies only in the naming; and \verb|M| (or \verb|M'|)
7332 may be embedded in \verb|N|, since \verb|N| defines an enlarged
7333 (two-sorted) structure. The following definitions are nothing more
7334 than the statements of the obvious, like this tiny example. They
7335 are complicated only because \cafeobj~is founded on a rich logical base.
7336
7337 \paragraph{Derived Operators}\index{derived operator}
7338
7339 A derived operator is just a term regarded as an operator.
7340 For example, under the module \verb|MULT| (Section \ref{exs:mult})
7341 and for variables \verb|X| and \verb|Y| of sort \verb|Nat|,
7342 \verb|s(0)|, \verb|s(s(X))|, ``\verb|s(0) + X|'', ``\verb|s(X) * s(Y)|'', and
7343 ``\verb|X * X|'' are all
7344 terms, hence derived operators. The important point here is that each
7345 such term can be assigned a rank, inductively. We have
7346 \begin{center}
7347 \begin{tabular}{ll}
7348 \multicolumn{1}{l}{term} & \multicolumn{1}{l}{rank} \\ \hline
7349 \verb|s(0)| & \verb|-> NzNat| \\
7350 \verb|s(s(X))| & \verb|Nat -> NzNat| \\
7351 \verb|s(0) + X| & \verb|Nat -> NzNat| \\
7352 \verb|s(X) * s(Y)| & \verb|Nat Nat -> Nat| \\
7353 \verb|X * X| & \verb|Nat -> Nat| \\
7354 \end{tabular}
7355 \end{center}
7356 It is easy\footnote{
7357 Actually, not so easy. You have to calculate a least sort.
7358 }
7359 to calculate the rank of a derived operator: the sorts of
7360 the variables constitute the arity, and the coarity of the dominant operator
7361 is the coarity.
7362
7363 Further, given a signature, the derived signature\index{derived signature}
7364 is the one containing every derived operator.
7365 Thus, if we denote derived operators by terms,
7366 the derived signature of \verb|MULT| looks like
7367 \begin{vvtm}
7368 \begin{ccode}
7369 [ Zero NzNat < Nat ]
7370 op 0 : -> Zero
7371 op s : Nat -> NzNat
7372 op _+_ : Nat Nat -> Nat
7373 op _*_ : Nat Nat -> Nat
7374 ...
7375 op s(0) : -> NzNat
7376 ...
7377 op s(s(X)) : Nat -> NzNat
7378 ...
7379 op s(0) + X : Nat -> Nat
7380 ...
7381 \end{ccode}
7382 \end{vvtm}
7383 which, of course, we cannot write out fully, since this signature is
7384 infinite.
7385
7386 \paragraph{Signature Morphisms}
7387
7388 A signature morphism\index{signature morphism} is a structure-preserving
7389 mapping from a signature to another. For example,
7390 \begin{vvtm}
7391 \begin{ccode}
7392 signature {
7393 [ S < S' ]
7394 *[ H ]*
7395 bop f : H -> S
7396 }
7397 \end{ccode}
7398 \end{vvtm}
7399 may be mapped to
7400 \begin{vvtm}
7401 \begin{ccode}
7402 signature {
7403 [ T < T', U ]
7404 *[ K < K' ]*
7405 bop g : K -> T
7406 }
7407 \end{ccode}
7408 \end{vvtm}
7409 via the mapping
7410
7411 %\begin{center}
7412 \begin{tabular}{lll}
7413 \verb|S| & $\to$ & \verb|T| \\
7414 \verb|S'| & $\to$ & \verb|T'| \\
7415 \verb|H| & $\to$ & \verb|K| \\
7416 \verb|f| & $\to$ & \verb|g| \\
7417 \end{tabular}
7418 %\end{center}
7419
7420 which preserves sort ordering and visibility, as well as ranks of
7421 operators.
7422
7423 To be definite, given signatures $\Sigma$ and $\Sigma^{\prime}$,
7424 a signature morphism is a pair of
7425 mappings $h_s$ (of sorts) and $h_o$ (of operators) such that
7426 \begin{itemize}
7427 \item $h_s$ preserves and reflects visibility, i.e. $S$ is visible
7428 (hidden) iff $h_s(S)$ is;
7429 \item $h_s$ preserves sort ordering, i.e. you must have
7430 $h_s(S) \leq h_s(S')$ if $S \leq S'$;
7431 \item $h_s$ reflects sort ordering on hidden sorts, i.e.
7432 you must have $S \leq S'$ if $h_s(S) \leq h_s(S')$;
7433 \item $h_o$ preserves ranks, i.e. an operator $f$ with rank
7434 $S_1 \ldots S_n \to S$ must be mapped to an operator
7435 with rank $h_s(S_1) \ldots h_s(S_n) \to h_s(S)$;
7436 \item $h_o$ maps behaviourals to behaviourals (\verb|bop| to \verb|bop|),
7437 and \verb|op| to \verb|op|; and
7438 \item $h_o$ is surjective on behaviourals, i.e. for any behavioural
7439 operator $g$ in the target signature,
7440 there must be a (behavioural) operator $f$ such that $h_o(f) = g$.
7441 \end{itemize}
7442
7443 \paragraph{Specification Morphism}\index{specification morphism}
7444
7445 Signature morphisms take care of signatures. Modules contain
7446 axioms as well, and specification morphisms are defined to
7447 ``preserve'' axioms. As an example, consider \verb|MONOID|
7448 (Section \ref{exs:monoid}) and \verb|SIMPLE-NAT|
7449 (Section \ref{exs:simple-nat}). If \verb|SIMPLE-NAT| indeed specifies
7450 natural number addition, it is associative and has identity \verb|0|,
7451 hence constitutes a monoidal structure. This fact can be confirmed by
7452 \begin{itemize}
7453 \item[1.] Specifying a mapping of sorts and operators that is a
7454 signature morphism, and
7455 \item[2.] Showing that, under that mapping, the (translated)
7456 axioms of \verb|MONOID| hold in \verb|SIMPLE-NAT|.
7457 \end{itemize}
7458 A specification morphism is a signature morphism that satisfies such
7459 conditions as 2. above. A precise definition would require the
7460 precise notion of satisfaction, which is beyond this exposition
7461 (note that you have to consider transition relations
7462 and behavioural equivalence, in addition to equality).
7463 In its place and for intuitive illustration, we shall elaborate on the above
7464 monoid example, after introducing concrete language constructs.
7465
7466 \subsection{View}\label{sec:p2-view}
7467
7468 A {\em view}\index{view} specifies ways to
7469 bind actual parameters to formal parameters.
7470 Given a parameter, i.e. a module $T$, and a module $M$,
7471 a view $V$ from $T$ to $M$ is a specification morphism
7472 from $T$ to $M'$, the derived specification of $M$, with one modification:
7473 the operator mapping must be surjective only for
7474 non-derived behavioural operators (i.e., those originally in $M$).
7475 To spell out the definitions of the previous section, therefore,
7476 a view is a pair of structure-preserving mappings of sorts
7477 and operators, under which axioms of $T$ hold in $M'$
7478 --- hence in $M$, since $M'$ does not contain extra axioms.
7479
7480 To instantiate a parameterised module, it is not enough to supply an
7481 actual parameter, since there are in general several ways to
7482 replace sorts with sorts and operators with operators. You need a view.
7483 \label{sec:p2-view-decl}
7484 A view declaration consists of a parameter module, an actual parameter module,
7485 and a set of maps that constitute a specification morphism.
7486
7487 \begin{bsyntax} view \Hline
7488 \index{\texttt{view}}\index{\texttt{from}}\index{\texttt{to}}
7489 \texttt{view} \textit{view\_name} \texttt{from} \textit{module\_name} \texttt{to} \textit{module\_name} ``\texttt{\{}''
7490 ~~~\textit{view\_element} $*_,$
7491 ``\texttt{\}}''
7492 \end{bsyntax}
7493
7494 \begin{tabular}{r c l}
7495 \textit{view\_element} &::=& \textit{map} \\
7496 &$|$& \textit{variable\_declaration}
7497 \end{tabular}
7498
7499 A view name is a character string. The module names after \verb|from|
7500 and \verb|to| are those of the parameter module and the actual parameter
7501 respectively. Maps are as follows.
7502
7503 \begin{tabular}{r c l}\index{\texttt{sort}}\index{\texttt{op}}\index{\texttt{hsort}}
7504 \index{\texttt{bop}}
7505 \textit{map} &::=& \verb|sort| \textit{sort\_name} ``\verb|->| \textit{sort\_name} \\
7506 &$|$& \verb|hsort| sort\_name ``\verb|->| sort\_name \\
7507 &$|$& \verb|op| \textit{operator\_reference} ``\verb|->|'' \textit{operator\_reference} \\
7508 &$|$& \verb|bop| \textit{operator\_reference} ``\verb|->|'' \textit{operator\_reference}
7509 \end{tabular}
7510
7511 A sort name may be qualified by a module name or a parameter name
7512 (Section \ref{sec:p2-naming-and-qualification}),
7513 and has to be visible for \verb|sort|, and hidden for \verb|hsort|.
7514 An operator reference is an operator name, or a term that denotes
7515 a derived operator. An operator reference may also be qualified. And
7516 needless to say, \verb|op| is for non-behavioural operators, and
7517 \verb|bop| is for behaviourals.
7518
7519 For a source operator, only operator names, or terms that denotes
7520 original operators, is allowed. Using the example of \verb|MULT|
7521 with variables \verb|X| and \verb|Y|,
7522 \begin{vvtm}
7523 \begin{ccode}
7524 _*_
7525 X * Y
7526 \end{ccode}
7527 \end{vvtm}
7528 are allowed, while
7529 \begin{vvtm}
7530 \begin{ccode}
7531 s(0)
7532 s(X) * Y
7533 X * X
7534 \end{ccode}
7535 \end{vvtm}
7536 are not.
7537
7538 A variable declaration is as in Section \ref{sec:p2-variable-decl}, like
7539 \begin{vvtm}
7540 \begin{ccode}
7541 vars X Y : Nat
7542 \end{ccode}
7543 \end{vvtm}
7544 where sort names are those in the {\em source} module.
7545
7546 \begin{warning}
7547 You may sometimes omit commas between view elements, but it is
7548 safer always to put them.
7549 \end{warning}
7550
7551 For a view declaration to define a specification morphism, the maps
7552 must satisfy the conditions for signature morphisms, and
7553 the actual parameter must satisfy the translated axioms of the formal
7554 (See \ref{sec:p2-pre-view}). And we remark that
7555 \begin{itemize}
7556 \item If operators are
7557 overloaded, a single map determines a correspondence between all the
7558 overloaded operators at once;
7559 \item A view may be endomorphic, and not necessarily be the identity mapping
7560 at that; and
7561 \item A view need not be onto, nor one-to-one.
7562 \end{itemize}
7563
7564 \begin{warning}
7565 The system only checks syntactic conditions. To check theory inclusion,
7566 you need a powerful theorem prover, which is lacking in the current
7567 system.
7568 \end{warning}
7569
7570 \begin{warning}
7571 Equational theory attributes (Section \ref{sec:p2-equational-theory})
7572 act like axioms when views are considered: associativity, for example,
7573 is an axiom that must hold in the actual parameter.
7574 % However,
7575 % for equational theory attributes, the system imposes a stronger requirement:
7576 % the same attributes must be specified for actual operators.
7577 In the spirit of checking only syntactic conditions, the system
7578 ignores these attributes.
7579 \end{warning}
7580
7581 \begin{warning}
7582 The system does not check every syntactic conditions.
7583 \end{warning}
7584
7585 \subsection{Views Galore}\label{sec:p2-views-exs}
7586
7587 We illustrate the definitions and remarks by several examples.
7588 A first example maps \verb|MONOID| to \verb|SIMPLE-NAT|
7589 (Sections \ref{exs:monoid} and \ref{exs:simple-nat}).
7590 \begin{vvtm}
7591 \begin{ccode}
7592 view NAT-AS-MONOID from MONOID to SIMPLE-NAT {
7593 sort M -> Nat,
7594 op e -> 0,
7595 op _*_ -> _+_
7596 }
7597 \end{ccode}
7598 \end{vvtm}
7599 This mapping is certainly a signature morphism, since,
7600 by replacing \verb|M| with \verb|Nat|, the ranks of
7601 \verb|e| and ``\verb|_*_|'' match \verb|0| and ``\verb|_+_|'' respectively.
7602 And we can show that, for variables \verb|X|, \verb|Y|, and \verb|Z|
7603 of sort \verb|Nat|, the equations
7604 \begin{vvtm}
7605 \begin{ccode}
7606 eq (X + Y) + Z = X + (Y + Z) .
7607 eq 0 + X = X .
7608 eq X + 0 = X .
7609 \end{ccode}
7610 \end{vvtm}
7611 hold under the theory of \verb|SIMPLE-NAT|
7612 (but this requires a non-trivial proof; we shall come to this later).
7613 Now consider another monoid, natural numbers under multiplication:
7614 \begin{vvtm}
7615 \begin{ccode}
7616 view NAT-AS-MONOID' from MONOID to MULT {
7617 sort M -> Nat,
7618 op e -> s(0),
7619 op _*_ -> _*_
7620 }
7621 \end{ccode}
7622 \end{vvtm}
7623 \verb|MULT| was in Section \ref{exs:mult}. Here \verb|M| is mapped to
7624 the same sort \verb|Nat| as before, but the operators are mapped
7625 differently. Note first that ``\verb|_*_|'' refers to two distinct operators:
7626 over \verb|M| and \verb|Nat|. Further note that the constant
7627 \verb|e| is mapped to \verb|s(0)|, which is {\em not} a constant declared
7628 in \verb|MULT|. It is a derived operator, and this view shows why
7629 a view is defined to be a mapping to the derived specification.
7630 To show that \verb|NAT-AS-MONOID'| defines a specification morphism, we
7631 need to prove
7632 \begin{vvtm}
7633 \begin{ccode}
7634 eq (X * Y) * Z = X * (Y * Z) .
7635 eq s(0) * X = X .
7636 eq X * s(0) = X .
7637 \end{ccode}
7638 \end{vvtm}
7639 Note, finally, that we may as well replace \verb|SIMPLE-NAT| by \verb|MULT|
7640 as the target of \verb|NAT-AS-MONOID|, since \verb|MULT| imports every
7641 declaration of \verb|SIMPLE-NAT|. This fact confirms the observation that
7642 there can be more than one views between two modules.
7643
7644 We now turn to somewhat artificial examples. For the sake of brevity, let
7645 us define a module without axioms:
7646 \begin{vvtm}
7647 \begin{ccode}
7648 module NAT-INT {
7649 [ Nat < Int ]
7650 op _+_ : Nat Nat -> Nat
7651 op _+_ : Int Int -> Int
7652 }
7653 \end{ccode}
7654 \end{vvtm}
7655 Then we have a view
7656 \begin{vvtm}
7657 \begin{ccode}
7658 view ID from NAT-INT to NAT-INT {
7659 sort Nat -> Nat,
7660 sort Int -> Int,
7661 op _+_ -> _+_
7662 }
7663 \end{ccode}
7664 \end{vvtm}
7665 which is just the pair of identity mappings. Note that the operator map
7666 denotes two maps, for ``\verb|_+_|'' on \verb|Nat| and that on \verb|Int|.
7667 This is an example of overloaded operators being bundled into one
7668 (Section \ref{sec:p2-operator-reference}).
7669
7670 We have other endomorphisms.
7671 \begin{vvtm}
7672 \begin{ccode}
7673 view U from NAT-INT to NAT-INT {
7674 sort Nat -> Nat,
7675 sort Int -> Nat,
7676 op _+_ -> _+_
7677 }
7678
7679 view L from NAT-INT to NAT-INT {
7680 sort Nat -> Int,
7681 sort Int -> Int,
7682 op _+_ -> _+_
7683 }
7684 \end{ccode}
7685 \end{vvtm}
7686 \verb|U| collapses \verb|Nat| and \verb|Int| into \verb|Nat|. Recall that the
7687 conditions on signature morphisms only require preservation of weak sort
7688 ordering. \verb|Nat < Nat| holds\footnote{
7689 Remember that \texttt{<} denotes less than \emph{or equal to}.
7690 }, so \texttt{U} does define a signature morphism. The case of \texttt{L} is
7691 similar, collapsing the two sorts into \texttt{Int}.
7692 Even with relatively simple structures like this,
7693 you have a couple of different views between modules.
7694
7695 \subsection{Not Quite a View}\label{sec:p2-non-view-exs}
7696
7697 We show why the conditions on views (see Section \ref{sec:p2-pre-view},
7698 on specification morphisms) are reasonable.
7699 It is easy to see why you have to preserve ranks and sort ordering.
7700 For example, given a module
7701 \begin{vvtm}
7702 \begin{ccode}
7703 module FUNCTION {
7704 [ S T ]
7705 op f : S -> T
7706 }
7707 \end{ccode}
7708 \end{vvtm}
7709 and a self-mapping
7710 \begin{vvtm}
7711 \begin{ccode}
7712 sort S -> T
7713 sort T -> S
7714 op f -> f
7715 \end{ccode}
7716 \end{vvtm}
7717 it is hard to know where to locate the image of \verb|f|.
7718
7719 The conditions on hidden sorts and behavioural operators ensure the
7720 preservation of visibility --- mapping hiddens to hiddens, behaviourals
7721 to behaviourals --- and observations. The following
7722 counterexample illustrates the reason why views must reflect
7723 hidden sort ordering and be surjective on behavioural operators.
7724 Recall the module \verb|COUNTER| (Section \ref{exs:counter}), and
7725 consider a module of bank accounts:\label{exs:account}
7726 \begin{vvtm}
7727 \begin{ccode}
7728 module SUBTRACT {
7729 protecting (SIMPLE-NAT)
7730 op _-_ : Nat Nat -> Nat
7731 vars N N' : Nat
7732 eq 0 - N = 0 .
7733 eq N - 0 = N .
7734 eq s(N) - s(N') = N - N' .
7735 }
7736
7737 module* ACCOUNT {
7738 protecting (SUBTRACT)
7739 *[ Account ]*
7740 bops deposit withdraw : Account Nat -> Account
7741 bops balance point : Account -> Nat
7742 var N : Nat
7743 var A : Account
7744 eq balance(deposit(A, N)) = balance(A) + N .
7745 eq balance(withdraw(A, N)) = balance(A) - N .
7746 eq point(deposit(A, N)) = point(A) + s(0) .
7747 }
7748 \end{ccode}
7749 \end{vvtm}
7750 ``\verb|_-_|'' on \verb|Nat| is the usual pseudo-subtraction
7751 (you have already seen this definition in Section \ref{exs:simple-nat+},
7752 as a preparation to declare \verb|GCD|).
7753 An account has a balance and a ``bonus point''. The bonus point
7754 increases each time you make a deposit. If you identify a counter
7755 reading with the amount of your fortune,
7756 it seems plausible to define a view, by the following mapping.
7757 \begin{vvtm}
7758 \begin{ccode}
7759 sort Zero -> Zero
7760 sort NzNat -> NzNat
7761 sort Nat -> Nat
7762 op 0 -> 0
7763 op s -> s
7764 op _+_ -> _+_
7765 var N : Nat
7766 var C : Counter
7767 hsort Counter -> Account
7768 bop add(N, C) -> deposit(C, N)
7769 bop read -> balance
7770 \end{ccode}
7771 \end{vvtm}
7772 Note first the appearance of \verb|Zero|, \verb|NzNat|, et al.
7773 A view defines a mapping of all the sorts and operators, including those
7774 declared in imported modules\footnote{
7775 Even implicitly imported modules. We ignore them here, for your health.
7776 }.
7777 As a result, views easily become large.
7778 And quite often, they contain a large uninformative set of maps --- the
7779 identitiy maps, like here. There is a way to cut them out. See Section
7780 \ref{sec:p2-succinct-view}.
7781
7782 Note also the usage of variables. In mapping
7783 \begin{vvtm}
7784 \begin{ccode}
7785 bop add : Nat Counter -> Counter
7786 \end{ccode}
7787 \end{vvtm}
7788 to
7789 \begin{vvtm}
7790 \begin{ccode}
7791 bop deposit : Account Nat -> Account
7792 \end{ccode}
7793 \end{vvtm}
7794 you need to change the order of arguments. Variables and derived opeartors
7795 are used to state this permutation. In the term \verb|deposit(C,N)|, the
7796 variable \verb|C| is interpreted as that of \verb|Account|, according to
7797 the sort map from \verb|Counter| to \verb|Account|.
7798
7799 Let us return to conditions on views. This mapping does not
7800 constitute a view, since
7801 \verb|withdraw| and \verb|point| are not in the image of the
7802 mapping. This oversight is harmful. It can be shown that in
7803 \verb|COUNTER| the behavioural equation
7804 \begin{vvtm}
7805 \begin{ccode}
7806 beq add(m + n, c) = add(n, add(m, c)) .
7807 \end{ccode}
7808 \end{vvtm}
7809 holds for any \verb|m|, \verb|n| of sort \verb|Nat| and for any
7810 \verb|c| of sort \verb|Counter|, while its counterpart
7811 \begin{vvtm}
7812 \begin{ccode}
7813 beq deposit(a, m + n) = deposit(deposit(a, m), n) .
7814 \end{ccode}
7815 \end{vvtm}
7816 in \verb|ACCOUNT| does not. The reason is that the observation
7817 \verb|point(A:Account)| distinguishes the two terms:
7818 thus behavioural equivalence is not preserved under this mapping ---
7819 even though the (single) axiom of \verb|COUNTER| is preserved.
7820
7821 You should have ensured that no additional observation is imposed on
7822 (the image of) \verb|Counter|. The conditions on views are sufficient
7823 for this purpose. And note well that, if you pay respect to the
7824 conditions, behavioural equivalence is preserved whenever
7825 the {\em explicitly given}, {\em finite} axioms are preserved.
7826
7827 \subsection{Succinct Views}\label{sec:p2-succinct-view}
7828
7829 Fully to spell out the necessary maps may lead to unwieldy views.
7830 In the previous section, you already get a hunch that imported sorts and
7831 operators induce explosion. In fact,
7832 it is possible to omit some obvious maps: the system guesses the
7833 missing pieces by the following rules. Unless explicitly written
7834 otherwise,
7835 \begin{itemize}
7836 \item[1.] If the source and target modules have common submodules,
7837 all the sorts and modules declared therein are assumed to
7838 be mapped to themselves;
7839 \item[2.] If the source and target modules have sorts and/or operators
7840 with identical names, they are mapped to their respective namesakes; and
7841 \item[3.] If the source module has a single sort and the target has
7842 a {\em principal} sort\index{principal sort}, the single sort is
7843 mapped to the principal.
7844 \end{itemize}
7845 The rules 1. and 2. are obviously helpful: they allow you to omit the obvious.
7846
7847 A principal sort is declared within the header part of
7848 a module declaration, as follows.
7849
7850 \begin{bsyntax} module with principal sort \index{\texttt{principal-sort}} \Hline
7851 \texttt{module} \textit{module\_name} $[$``\texttt{(}'' \textit{list\_of\_parameters} ``\texttt{)}''$]$ \texttt{principal-sort} \textit{sort\_name} ``\texttt{\{}''
7852 ~~~~\textit{module\_element} $*$
7853 ``\texttt{\}}''
7854 \end{bsyntax}
7855
7856 \verb|principal-sort| may be abbreviated to \verb|p-sort|.
7857 \index{\texttt{p-sort}}
7858 A sort name refers to a sort that is declared within the module, or in an
7859 imported module.
7860 Let us show some examples. If you have
7861 \begin{vvtm}
7862 \begin{ccode}
7863 module* ONE { [ Elt ] }
7864 \end{ccode}
7865 \end{vvtm}
7866 and
7867 \begin{vvtm}
7868 \begin{ccode}
7869 module! SIMPLE-NAT' principal-sort Nat {
7870 ... the same as SIMPLE-NAT
7871 }
7872 \end{ccode}
7873 \end{vvtm}
7874 it is possible to declare a view
7875 \begin{vvtm}
7876 \begin{ccode}
7877 view V from ONE to SIMPLE-NAT' { }
7878 \end{ccode}
7879 \end{vvtm}
7880 which apparently is an empty mapping. This is equivalent to
7881 \begin{vvtm}
7882 \begin{ccode}
7883 view V from ONE to SIMPLE-NAT' { sort Elt -> Nat }
7884 \end{ccode}
7885 \end{vvtm}
7886 Since great many views are from single-sort modules,
7887 principal sorts are very useful for making views compact.
7888
7889 If so declared, a sort is principal in and only in that module, and
7890 principality is {\em not} obtainable via importation\footnote{
7891 You may buy out Monaco, but it is another matter.
7892 }.
7893
7894 \begin{warning}
7895 The current version does not support all the mechanisms explained
7896 here. Identifying sorts or operators by name
7897 is sometimes tedious --- you may have to probe the depth of the module
7898 hierarchy. Or controvertial. Consider, e.g.,
7899 ``\verb|_+_|'' for natural number addition and for direct summation of sets.
7900 \end{warning}
7901
7902 It is also possible to shorten views by importation.
7903 \index{importation (of views)}
7904
7905 \begin{warning}
7906 At the time of writing, it is not possible. We have not even worked
7907 out a relevant syntax.
7908 \end{warning}
7909
7910 \section{Binding Parameters}
7911
7912 \subsection{Instantiation of Parameterised Modules}
7913 \label{sec:p2-module-instantiation}
7914
7915 An instance of a parameterised module is created by
7916 binding actual parameters to formals. The process of binding is
7917 called {\em instantiation}\index{instantiation}.
7918 The result of instantiation is a new module, obtained by
7919 replacing occurrences of parameter sorts and operators by actuals.
7920 If, as a result of instantiation, a module is imported twice,
7921 it is assumed to be imported once, and shared throughout
7922 (Section \ref{sec:p2-module-sharing}).
7923
7924 Syntactically, instantiation binds a view to a parameter.
7925
7926 \begin{bsyntax} instantiating parameterised module \Hline
7927 \textit{module\_name} ``\texttt{(}'' \textit{binding} $+_,$ ``\texttt{)}''
7928 \end{bsyntax}
7929
7930 There are two distinct kinds of binding constructs.
7931
7932 \paragraph{Instantiation by Declared Views}
7933
7934 You may bind an already declared view to a parameter.
7935
7936 \begin{tabular}{r c l}\index{\texttt{<=}}
7937 \textit{binding} &::=& \textit{parameter\_name} ``\verb|<=|'' \textit{view\_name}
7938 \end{tabular}
7939
7940 If a module \verb|M| has a parameter ``\verb|X :: T|'' and a view
7941 \verb|V| from \verb|T| to \verb|M'| is declared,
7942 \verb|V| may be bound to \verb|X|, with the effect that
7943 \begin{itemize}
7944 \item[1.] The sort and operator names of \verb|T|
7945 that appear in the body of \verb|M| are replaced by
7946 those in \verb|M'|, in accordance with \verb|V|, and
7947 \item[2.] The common submodules of \verb|M| and \verb|M'| are shared.
7948 \end{itemize}
7949
7950 \paragraph{Instantiation by Ephemeral Views}
7951
7952 It is possible to declare and bind a view simultaneously.
7953
7954 \begin{tabular}{r c l}\index{\texttt{view}}\index{\texttt{to}}
7955 \textit{binding} &::=& \textit{parameter\_name} ``\verb|<=|'' \textit{view} \\
7956 \textit{view} &::=& \verb|view| \verb|to| \textit{module\_name}
7957 ``\verb|{|'' \textit{view\_element} $*$ ``\verb|}|''
7958 \end{tabular}
7959
7960 where ``\verb|{|'', ``\verb|}|'' encloses the same constructs as in view
7961 declarations. The view is valid only within the instantiation.
7962 For example,\label{exs:nat-ilist}
7963 \begin{vvtm}
7964 \begin{ccode}
7965 module NAT-ILIST {
7966 protecting (ILIST(IDX <= view to SIMPLE-NAT { sort Elt -> Nat },
7967 DAT <= view to SIMPLE-NAT { sort Elt -> Nat }))
7968 }
7969 \end{ccode}
7970 \end{vvtm}
7971 binds a \verb|SIMPLE-NAT| to the parameter \verb|IDX| and \verb|DAT|,
7972 via an identical view from \verb|ONE| (see Section \ref{exs:simple-nat} for
7973 \verb|SIMPLE-NAT|, Section \ref{exs:ilist} for
7974 \verb|ILIST| and \verb|ONE|). The code is equivalent to
7975 \begin{vvtm}
7976 \begin{ccode}
7977 view V from ONE to SIMPLE-NAT { sort Elt -> Nat }
7978
7979 module NAT-ILIST {
7980 protecting (ILIST(IDX <= V, DAT <= V))
7981 }
7982 \end{ccode}
7983 \end{vvtm}
7984 An ephemeral view may be as succinct as a everlasting counterpart, with
7985 resort to principality and titular coincidence.
7986
7987 To make the code more concise, it is possible to identify parameters
7988 by positions, not by names. \verb|NAT-ILIST| may then be defined by
7989 \begin{vvtm}
7990 \begin{ccode}
7991 module NAT-ILIST {
7992 protecting (ILIST(V,V))
7993 }
7994 \end{ccode}
7995 \end{vvtm}
7996 or, more ephemerally,
7997 \begin{vvtm}
7998 \begin{ccode}
7999 module NAT-ILIST {
8000 protecting (ILIST(SIMPLE-NAT { sort Elt -> Nat },
8001 SIMPLE-NAT { sort Elt -> Nat }))
8002 }
8003 \end{ccode}
8004 \end{vvtm}
8005 Note that in the latter code ``\verb|view to|'' constructs are omitted.
8006
8007 \subsection{Parameters as Imports}\label{sec:p2-params-as-subs}
8008
8009 A parameter may be regarded as a submodule. If so regarded,
8010 it poses a question of importation modes (Section \ref{sec:p2-import-decl})
8011 and module contexts (Section \ref{sec:p2-current-module}).
8012 Indeed, it is possible to specify an importation mode for a parameter, as in
8013 \begin{vvtm}
8014 \begin{ccode}
8015 module M (extending X :: T) \{ ...
8016 \end{ccode}
8017 \end{vvtm}
8018 Available modes are the same as with plain importations: \verb|protecting|,
8019 \verb|extending|, and \verb|using|. The default mode is \verb|protecting|.
8020 You can check by feeding \verb|ILIST| (Section \ref{exs:ilist}) and
8021 print parameters as follows.
8022 \begin{vvtm}
8023 \begin{ccode}
8024 CafeOBJ> select ILIST
8025
8026 ILIST(IDX,DAT)> show params
8027 argument IDX : protecting ONE
8028 argument DAT : protecting ONE
8029
8030 ILIST(IDX,DAT)>
8031 \end{ccode}
8032 \end{vvtm}
8033 Note in passing that the new prompt shows parameters as well as the module
8034 name \verb|ILIST|.
8035
8036 %\begin{warning}
8037 % The current version is very verbose and print formal parameter modules
8038 % as well, as in
8039 % \begin{vvtm}
8040 %\begin{ccode}
8041 % ILIST(IDX::ONE ,DAT::ONE)>
8042 % \end{ccode}
8043 %\end{vvtm}
8044 %\end{warning}
8045
8046 \begin{warning}
8047 As stated in Section \ref{sec:p2-import-decl}, the system does not
8048 care if you have chosen an incorrect importation mode. Since
8049 various modules are to be bound to parameters, you should be especially
8050 cafeful.
8051 \end{warning}
8052
8053 As to contexts, a parameter is included in the context graph of the module.
8054 The difference from plainly imported modules is that the edge has
8055 a name. For example, if \verb|M| imports \verb|M'|, the edge is anonymous
8056 \begin{quotation}
8057 \verb|M| $\longrightarrow$ \verb|M'|
8058 \end{quotation}
8059 while if \verb|M| has a parameter ``\verb|X :: M'|'', the edge is
8060 \begin{quotation}
8061 \texttt{M} $\buildrel \mbox{\texttt{X}} \over \longrightarrow$ \texttt{M'}
8062 \end{quotation}
8063 Moreover, since a module may have the same parameter with different names,
8064 a context graph is in general a hypergraph.
8065
8066 Some aspects of instantiation is better explained in terms of such graphs.
8067 Instantiation is then a process of subgraph replacement, and the
8068 usual considerations --- indentifying which edges with which,
8069 determining targets of edges, avoiding dangling edges, and so on --- are
8070 relevant to the process of instantiation.
8071
8072 \subsection{Parameter Passing}
8073
8074 Since a parameterised module is a module, it may be imported.
8075 You may enrich \verb|ILIST| (Section \ref{exs:ilist}), adding
8076 an operator that counts entries.
8077 \begin{vvtm}
8078 \begin{ccode}
8079 module ILIST-EXT {
8080 protecting (ILIST)
8081 protecting (SIMPLE-NAT)
8082 op #_ : Ilist -> Nat
8083 var I : Elt.IDX
8084 var D : Elt.DAT
8085 var L : Ilist
8086 eq # put(I,D,L) = s(0) + (# L) .
8087 eq # empty = 0 .
8088 }
8089 \end{ccode}
8090 \end{vvtm}
8091 The definition of ``\verb|#_|'' is obvious, but this module is
8092 interesting in that it imports a parameterised module without
8093 instantiation. Let us see how the system treats such a module.
8094 After feeding \verb|ILIST-EXT|, you get
8095 \begin{vvtm}
8096 \begin{ccode}
8097 CafeOBJ> select ILIST-EXT
8098
8099 ILIST-EXT(IDX, DAT)> show params
8100 argument IDX.ILIST : protecting ONE
8101 argument DAT.ILIST : protecting ONE
8102
8103 ILIST-EXT(IDX, DAT)> show param IDX
8104 module* IDX.ILIST :: ONE {
8105 imports {
8106 protecting (BOOL)
8107 }
8108 signature {
8109 [ Elt ]
8110 }
8111 }
8112
8113 ILIST-EXT(IDX, DAT)>
8114 \end{ccode}
8115 \end{vvtm}
8116 Look first at the new prompt. In the previous section, after
8117 the parameterised \verb|ILIST| was \verb|select|ed, the prompt
8118 changed to
8119 \begin{vvtm}
8120 \begin{ccode}
8121 ILIST(IDX, DAT)>
8122 \end{ccode}
8123 \end{vvtm}
8124 to indicate that the current module had two parameters. The \verb|select|ion
8125 here of \verb|ILIST-EXT| also led to the prompt suggesting parameters.
8126 As the \verb|show| commands showed, this module indeed has parameters
8127 \verb|IDX| and \verb|DAT|, which are both the module \verb|ONE|.
8128 %The new prompt indicates that these parameters were derived from
8129 %\verb|ILIST|.
8130
8131 %\begin{warning}
8132 % The current version is very verbose, and in the above case the prompt is
8133 % \begin{vvtm}
8134 %\begin{ccode}
8135 % ILIST-EXT(IDX.ILIST::ONE, DAT.ILIST::ONE)>
8136 % \end{ccode}
8137 %\end{vvtm}
8138 %\end{warning}
8139
8140 Since \verb|ILIST-EXT| is thus parameterised, you can instantiate it,
8141 just as you instantiate \verb|ILIST|. For example,
8142 \begin{vvtm}
8143 \begin{ccode}
8144 module NAT-ILIST-EXT {
8145 protecting (ILIST-EXT(IDX <= view to SIMPLE-NAT { sort Elt -> Nat },
8146 DAT <= view to SIMPLE-NAT { sort Elt -> Nat }))
8147 }
8148 \end{ccode}
8149 \end{vvtm}
8150 defines a module of enriched indexed lists whose indices and data are
8151 both natural numbers.
8152
8153 In importing parameterised modules, you can bind parameters to
8154 parameters --- ``passing parameters'', as in the following
8155 construction.
8156 \begin{vvtm}
8157 \begin{ccode}
8158 module ILIST' (X :: ONE) {
8159 protecting (ILIST(X, X))
8160 }
8161 \end{ccode}
8162 \end{vvtm}
8163 Remember that this shorthand notation means that the default view ---
8164 i.e., identity mapping ---, from
8165 \verb|X| (i.e. \verb|ONE|) to \verb|IDX| and \verb|DAT| (both \verb|ONE|)
8166 is bound to the parameters \verb|IDX| and \verb|DAT|. The result of the
8167 instantiation is the sort of indexed lists whose indices and data
8168 belong to the same (parametric) sort. Let us show a session that reveals
8169 what is going on.
8170 \begin{vvtm}
8171 \begin{ccode}
8172 CafeOBJ> module ILIST' (X :: ONE) {
8173 protecting (ILIST(X, X))
8174 }
8175 -- defining module ILIST'_.*.,,,,,,_,,*._* done.
8176
8177 CafeOBJ> select ILIST'
8178
8179 ILIST'(X)> show params
8180 argument X : protecting ONE
8181
8182 ILIST'(X)> show subs
8183 protecting(ILIST(IDX <= X, DAT <= X))
8184
8185 ILIST'(X)>
8186 \end{ccode}
8187 \end{vvtm}
8188 The module \verb|ILIST'| has a parameter \verb|X| and imports an
8189 instance of \verb|ILIST| to whose parameters \verb|X| are bound.
8190 We continue the session and create an instance of \verb|ILIST'|.
8191 \begin{vvtm}
8192 \begin{ccode}
8193 ILIST'(X)> module ILIST'-NAT {
8194 pr (ILIST'(X <= view to SIMPLE-NAT { sort Elt -> Nat }))
8195 }
8196
8197 -- defining module ILIST'-NAT,,,,,,,*,,,,,,,*._* done.
8198 ILIST'(X)>
8199 \end{ccode}
8200 \end{vvtm}
8201 \verb|ILIST'-NAT| is a module of indexed lists whose indices and data
8202 are both natural numbers --- which is the same module as
8203 \verb|ILIST-NAT| in the previous section.
8204
8205 Finally, you may use this parameter passing mechanism to get
8206 an alternative definition of enriched indexed lists, as follows.
8207 \begin{vvtm}
8208 \begin{ccode}
8209 module ILIST-EXT (IDX :: ONE, DAT :: ONE) {
8210 protecting (ILIST(IDX, DAT))
8211 protecting (SIMPLE-NAT)
8212 op #_ : Ilist -> Nat
8213 var I : Elt.IDX
8214 var D : Elt.DAT
8215 var L : Ilist
8216 eq # put(I,D,L) = s(0) + (# L) .
8217 eq # empty = 0 .
8218 }
8219 \end{ccode}
8220 \end{vvtm}
8221
8222 \subsection{Parameters with Parameters}
8223
8224 There are also cases when parameter modules themselves are parameterised.
8225 Consider the following example.
8226 \begin{vvtm}
8227 \begin{ccode}
8228 module* ONE { [ Elt ] }
8229 module* STACK (X :: ONE) {
8230 [ Stack ]
8231 op push : Elt Stack -> Stack
8232 }
8233
8234 module TRAY (Y :: STACK) {
8235 [ Tray ]
8236 op tray : Stack Stack -> Tray
8237 ops in-tray out-tray : Tray -> Stack
8238 op in : Elt Tray -> Tray
8239 var E : Elt
8240 vars S S' : Stack
8241 eq in(E, tray(S,S')) = tray(push(E,S),S') .
8242 trans tray(push(E,S),S') => tray(S, push(E, S')) .
8243 eq in-tray(tray(S,S')) = S .
8244 eq out-tray(tray(S,S')) = S' .
8245 }
8246 \end{ccode}
8247 \end{vvtm}
8248 The module \verb|TRAY| is intended to describe the desk of an
8249 important executive. The parameter \verb|STACK| of this
8250 module itself has a parameter \verb|ONE| (the same as the one
8251 in Section \ref{exs:ilist}).
8252
8253 There are two ways to instantiate \verb|TRAY|:
8254 \begin{itemize}
8255 \item[a.] Instantiate \verb|Y|, or
8256 \item[b.] Instantiate \verb|X| of \verb|Y|.
8257 \end{itemize}
8258 In case of a., \verb|Y| should be bound by a module which has
8259 the same module \verb|ONE| as parameter. For example,
8260 if you have defined
8261 \begin{vvtm}
8262 \begin{ccode}
8263 module LIST (X :: ONE) {
8264 [ NeList < List ]
8265 op nil : -> List
8266 op __ : Elt List -> NeList
8267 op __ : Elt NeList -> NeList
8268 op head_ : NeList -> Elt
8269 op tail_ : NeList -> List
8270 var E : Elt
8271 var L : List
8272 eq head (E L) = E .
8273 eq tail (E L) = L .
8274 }
8275 \end{ccode}
8276 \end{vvtm}
8277 you may bound \verb|LIST|\footnote{
8278 It has been a ritual to embed elements as one-length lists with
8279 a subsort declaration. This practice is not recommended since you
8280 may unable to get pushouts as desired. See the Haxhausen's paper
8281 in AMAST'96 for detail.
8282 } to the parameter \verb|Y|, as
8283 \begin{vvtm}
8284 \begin{ccode}
8285 view LIST-AS-STACK from STACK to LIST {
8286 sort Stack -> NeList,
8287 op push -> __
8288 }
8289
8290 module LIST-TRAY {
8291 protecting (TRAY(Y <= LIST-AS-STACK))
8292 }
8293 \end{ccode}
8294 \end{vvtm}
8295 The result is a module with parameter \verb|X| (originated from \verb|LIST|).
8296 This module defines trays of lists of \verb|Elt|. Note that
8297 \verb|LIST-AS-STACK| omits the map from \verb|Elt| of \verb|X| to itself.
8298 This is an example of implicit submodule mapping
8299 (cf. Section \ref{sec:p2-succinct-view}).
8300 You may further instantiate \verb|LIST-TRAY|, by binding something to
8301 \verb|X| --- which is \verb|ONE|, the module of an arbitrary set.
8302
8303 In case of b., you bind something to \verb|X| (of \verb|STACK|), which
8304 is the module \verb|ONE|. For example,
8305 \begin{vvtm}
8306 \begin{ccode}
8307 module NAT-TRAY {
8308 protecting (TRAY(X.Y <= view to SIMPLE-NAT { sort Elt -> Nat }))
8309 }
8310 \end{ccode}
8311 \end{vvtm}
8312 creates a module whose trays consist of
8313 stacks of natural numbers ---
8314 trays of mathematicians, perhaps. Note that \verb|X.Y| is a
8315 qualification of a parameter name by another parameter name
8316 (cf. Section \ref{sec:p2-naming-and-qualification}).
8317 %You may further instantiate \verb|NAT-TRAY|, by binding something
8318 %to \verb|Y| --- which is a module of natural number stacks.
8319 \verb|NAT-TRAY| is not parametric; as a result of the instantiation,
8320 \verb|STACK| becomes in effect a plain submodule of \verb|NAT-TRAY|.
8321
8322 %\begin{warning}
8323 % The current implementation does not work as explained. After creating
8324 % \verb|NAT-TRAY|, no parameter is left.
8325 %\end{warning}
8326
8327 \subsection{Qualifying Parameter Names}
8328
8329 Parameter names may conflict, when a parameterised module imports
8330 another parameterised module. To disambiguate parameter names, you may
8331 qualify them by module names
8332 (Section \ref{sec:p2-naming-and-qualification}). For example, in
8333 \begin{vvtm}
8334 \begin{ccode}
8335 module A ( X :: B ) { ... }
8336 module C ( X :: D ) {
8337 protecting (A)
8338 ...
8339 }
8340 \end{ccode}
8341 \end{vvtm}
8342 \verb|X| is both the parameter name of \verb|A| and that of \verb|C|,
8343 and is ambiguous in \verb|C|. You may refer to \verb|X| of \verb|A|
8344 by ``\verb|X.A|''. The plain \verb|X|, in \verb|C|, refers to \verb|X|
8345 of \verb|C|. This means that a sort or operator name may be
8346 qualified by a parameter name which itself is qualified by a module
8347 name. Since a term may be qualified by a sort name also
8348 (see Section \ref{sec:p2-parsing-qualifier}), a chain of qualifications
8349 such as
8350 \begin{vvtm}
8351 \begin{ccode}
8352 (f(a)):S.P.M
8353 \end{ccode}
8354 \end{vvtm}
8355 is possible. If there is any ambiguity, qualifier ``\verb|.|'' is
8356 considered right associative.
8357
8358 \section{Module Expression}\label{sec:p2-module-expression}
8359
8360 Syntactically, an instantiation of parameterised modules is an expression
8361 involving module names. There are other kinds of such expressions.
8362 Expressions over module names are called {\em module expression\/}s.
8363 \index{module expression}
8364 A module expression represents a module,
8365 and may appear in import declarations or in several commands.
8366 Here we summarise the syntax of module expressions.
8367
8368 \subsection{Module Name}
8369
8370 The simplest module expressions are module names.
8371
8372 \begin{bsyntax} module constant \Hline
8373 \textit{module\_name}
8374 \end{bsyntax}
8375
8376 A module represented by a module name is the module itself.
8377
8378 \subsection{Renaming}
8379
8380 {\em Renaming}\index{renaming} is to change sort or operator
8381 names, creating a new module.
8382
8383 \begin{bsyntax} renaming \index{\texttt{*} (for remaning)} \Hline
8384 \textit{module\_expression} ``\texttt{*}'' ``\texttt{\{}'' \textit{map} $*_,$ ``\texttt{\}}''
8385 \end{bsyntax}
8386
8387 where a map is
8388
8389 \index{\texttt{sort}}\index{\texttt{op}}\index{\texttt{param}}
8390 \index{\texttt{hsort}}\index{\texttt{bop}}
8391 \begin{tabular}{r c l}
8392 \textit{map} &::=& \verb|sort| \textit{sort\_name} ``\verb|->|'' \textit{sort\_name} \\
8393 &$|$& \verb|hsort| \textit{sort\_name} ``\verb|->|'' \textit{sort\_name} \\
8394 &$|$& \verb|op| \textit{operator\_name} ``\verb|->|'' \textit{operator\_name} \\
8395 &$|$& \verb|bop| \textit{operator\_name} ``\verb|->|'' \textit{operator\_name} \\
8396 \end{tabular}
8397
8398 Maps are as expected: \verb|sort| maps visible sorts, \verb|hsort| invisibles,
8399 \verb|op| ordinary operators, \verb|bop| behaviourals, and \verb|param|
8400 parameters. Sort and operator names should
8401 be familiar now. Source names may be qualified; target names may not
8402 (they are supposed to be brand-new names).
8403
8404 \begin{warning}
8405 Commas may be omitted.
8406 \end{warning}
8407
8408 Note that renaming creates a new, different module, even if it has
8409 isomorphic models. A consequence is that a renamed module is not
8410 shared (cf. Section \ref{sec:p2-module-sharing}).
8411
8412 \begin{warning}
8413 Renaming is meant to describe a bijective signature
8414 morphism (Section \ref{sec:p2-pre-view}). The current implementation allows
8415 more flexible renaming, however, and you may collapse
8416 two distinct sort/operators into one. If you do give such a renaming,
8417 care should be taken.
8418 \end{warning}
8419
8420 Renaming is quite often used in combination with instantiations.
8421 Since instantiation is a (module-level) operation that generates
8422 specific data types from generic ones, it is natural for you to
8423 confer names that describe specialised sorts and operators more
8424 effectively. For example, in specialising generic indexed lists to those of
8425 natural numbers (Section \ref{exs:nat-ilist}), you may write
8426 \begin{vvtm}
8427 \begin{ccode}
8428 module NAT-ILIST {
8429 protecting (ILIST(IDX <= view to SIMPLE-NAT { sort Elt -> Nat },
8430 DAT <= view to SIMPLE-NAT { sort Elt -> Nat })
8431 * { sort IList -> IList-of-Nat })
8432 }
8433 \end{ccode}
8434 \end{vvtm}
8435 In the parameterised \verb|ILIST|, a simple \verb|IList| is okay to
8436 denote lists of something. After instantiation, a list is a list
8437 of natural numbers, so why not call it as it is.
8438
8439 \subsection{Module Sum}
8440
8441 Combining several modules together is another common module-level
8442 operation. The combined whole is called {\em module sum}\index{module sum}.
8443
8444 \begin{bsyntax} module sum \index{\texttt{+} (module sum)} \Hline
8445 \textit{module\_expression} ``\texttt{+}'' \textit{module\_expression} $\{$ ``\texttt{+}'' \textit{module\_expression} $\}$ $*$
8446 \end{bsyntax}
8447
8448 A module represented by a module sum consists of all the module elements in
8449 the summands. If submodules are imported more than once, it is assumed to
8450 be imported once, and is shared throughout
8451 (Section \ref{sec:p2-module-sharing}).
8452 This construction is sometimes called {\em amalgamated sum} or
8453 {\em shared sum}.\index{amalgamated sum}\index{shared sum}
8454 From this definition, it follows that
8455 \begin{vvtm}
8456 \begin{ccode}
8457 module A { protecting (B + C) ... }
8458 \end{ccode}
8459 \end{vvtm}
8460 is equivalent to
8461 \begin{vvtm}
8462 \begin{ccode}
8463 module A { protecting (B) protecting (C) ... }
8464 \end{ccode}
8465 \end{vvtm}
8466
8467 \subsection{Making Modules}\label{sec:p2-make-command}
8468
8469 Module expressions are often used in isolation. To create a module, it is
8470 often the case that you simply import a module represented by an expression,
8471 as
8472 \begin{vvtm}
8473 \begin{ccode}
8474 module LOVE {
8475 protecting (...)
8476 }
8477 \end{ccode}
8478 \end{vvtm}
8479 where ``\verb|...|'' is a module expression. The purpose of this
8480 declaration is to give a name to a module defined by the expression.
8481 C'est tout. In such a case, an abbreviation is handy.
8482
8483 \begin{bsyntax} \texttt{make} command \index{\texttt{make}} \Hline
8484 \texttt{make} \textit{module\_name} ``\texttt{(}'' \textit{module\_expression} ``\texttt{)}''
8485 \end{bsyntax}
8486
8487 \verb|make| evaluates the module expression and confer the resulting
8488 module with the given name.
8489 In fact, this is a shorthand of the protecting importation as above.
8490 You may want to try this:
8491 \begin{vvtm}
8492 \begin{ccode}
8493 CafeOBJ> make LOVE (SIMPLE-NAT)
8494
8495 -- defining module LOVE._* done.
8496 CafeOBJ> show LOVE
8497 module LOVE {
8498 imports {
8499 protecting (SIMPLE-NAT)
8500 }
8501 }
8502
8503 CafeOBJ>
8504 \end{ccode}
8505 \end{vvtm}
8506
8507
8508 %\subsection{Rules with Hidden Sorts}
8509 %
8510 %% but should this be mentioned in the manual?
8511 %
8512 %In \verb|NAT-COUNTER|, \verb|Counter| acquires a
8513 %non-trivial reachability relation. For example,
8514 %\begin{vvtm}
8515 %\begin{ccode}
8516 % add(s(0), init)
8517 %\end{ccode}
8518 %\end{vvtm}
8519 %may be reached from
8520 %\begin{vvtm}
8521 %\begin{ccode}
8522 % add(s(0) | s(s(0)), init)
8523 %\end{ccode}
8524 %\end{vvtm}
8525 %The reasoning is as follows.
8526 %\begin{itemize}
8527 %\item[] \verb?add(s(0) | s(s(0)), init)? reads as \verb?s(0) | s(s(0)) + 0?,
8528 % from two equations in \verb|COUNTER|.
8529 %\item[] \verb?s(0) | s(s(0)) + 0? equals \verb?s(0) | s(s(0))?
8530 % since \verb|0| is the identity.
8531 %\item[] Similarly, \verb?add(s(0), init)? reads as \verb|s(0)|.
8532 %\item[] More generally, every context reads
8533 % \verb?n + s(0) | s(s(0))? and \verb|n + s(0)| respectively,
8534 % for the same \verb|n|.
8535 %\item[] For any \verb|n|, \verb|n + s(0)| is
8536 % reachable from \verb?n + s(0) | s(s(0))?, by a rule in \verb|HUMANE-NAT|
8537 % and because reachability is congruent.
8538 %\item[] Therefore, in every context, the read of
8539 % \verb?add(s(0), init)? is reachable from
8540 % \verb?add(s(0) | s(s(0)), init)?.
8541 %\item[] Therefore, behaviourally, the result holds.
8542 %\end{itemize}
8543 %
8544 %By the technique explained in \ref{sec:p2-open-close} as adapted to
8545 %coinduction scheme, it is possible to realise the above reasoning
8546 %as a proof score in \cafeobj.
8547 %
8548 %Beware, however, that
8549 %na{\" \i}ve combination of a set of equations and that of
8550 %rules may result in an undesirable proof. For example,
8551 %an indiscriminate use of rules and equations results in the
8552 %following rewrite sequence.
8553 %\begin{vvtm}
8554 %\begin{ccode}
8555 % read(add(s(0) | s(s(0)), init)) -->
8556 % read(add(s(0), init)) -->
8557 % s(0) + read(init) -->
8558 % s(0)
8559 %\end{ccode}
8560 %\end{vvtm}
8561 %where the first rewrite uses a rule in \verb|HUMANE-NAT|. This
8562 %reduction cannot be used to reason about \verb|Counter|'s behaviour,
8563 %since the context was changed before observation.
8564 %In general, in reasoning about behavioural equivalence or
8565 %reachability, you should use \verb|reduce|, which exludes rules,
8566 %instead of \verb|exec|, which does not.
8567
8568 \chapter{Theorem-Proving Tools}\label{sec:p2-theorem-proving}
8569
8570 The TRS engine (Chapter \ref{sec:p2-term-evaluation}) may be used as a
8571 lightweight theorem prover. In addition, the current version of the
8572 system supports a couple of commands that act as theorem-proving
8573 tools.
8574
8575 \section{Open/Closing Modules}\label{sec:p2-open-close}
8576
8577 There are commands to modify module declarations. To start modifying a
8578 module, use the command
8579
8580 \begin{bsyntax} \texttt{open} command \index{\texttt{open}} \Hline
8581 \texttt{open} \textit{module\_name}
8582 \end{bsyntax}
8583
8584 This command enables you to declare new sorts, operators, equations,
8585 and so on. Just type declarations after this command.
8586 You cannot, however, change or delete the existing declarations.
8587
8588 To finish the modification, use the command
8589
8590 \begin{bsyntax} \texttt{close} command \index{\texttt{close}} \Hline
8591 \texttt{close}
8592 \end{bsyntax}
8593
8594 \subsection{Why Opening Modules?}\label{sec:p2-usage-of-open}
8595
8596 The effects of declarations supplied in between \verb|open| and
8597 \verb|close| commands are temporary.
8598 \begin{itemize}
8599 \item An \verb|open| command creates a new module. This module contains
8600 a copy of declarations in the given module.
8601 \item Until the module is \verb|close|d, all the declared sorts,
8602 equations, etc. are added to the new module.
8603 \item \verb|close| expunges the new module.
8604 \end{itemize}
8605 So \verb|close| makes all your efforts banish. What is the sense of it all?
8606 Well, this open/closing mechanism is an ecological tool to (1) make
8607 hypotheses,
8608 (2) prove, by evaluation, your favourite theorems upon the hypotheses,
8609 (3) finish the proof, and delete the hypotheses, (1') start again with
8610 new hypotheses, \ldots
8611
8612 For example, consider the familiar module \verb|SIMPLE-NAT|, reprinted here.
8613 \begin{vvtm}
8614 \begin{ccode}
8615 module SIMPLE-NAT {
8616 [ Zero NzNat < Nat ]
8617 op 0 : -> Zero
8618 op s : Nat -> NzNat
8619 op _+_ : Nat Nat -> Nat
8620 vars N N' : Nat
8621 eq 0 + N = N .
8622 eq s(N) + N' = s(N + N') .
8623 }
8624 \end{ccode}
8625 \end{vvtm}
8626 Suppose you want to show that \verb|0| is a right identity of ``\verb|_+_|''
8627 also. Using the standard structural induction\index{structural induction},
8628 you can prove it easily, by
8629 showing (1) ``\verb|0 + 0|'' equals \verb|0|, and (2) for any \verb|M|,
8630 if ``\verb|M + 0|'' equals \verb|M|, \newline
8631 ``\verb|s(M) + 0|'' equals \verb|s(M)|.
8632 A score of this proof may be written, using \verb|open| command, as
8633 \begin{vvtm}
8634 \begin{ccode}
8635 open SIMPLE-NAT
8636 op a : -> Nat .
8637 eq a + 0 = a .
8638 reduce 0 + 0 .
8639 reduce s(a) + 0 .
8640 close
8641 \end{ccode}
8642 \end{vvtm}
8643 When a module is \verb|open|ed, the system changes the prompt beginning
8644 with \verb|%|, as
8645 \begin{vvtm}
8646 \begin{ccode}
8647 CafeOBJ> open SIMPLE-NAT
8648
8649 -- opening module SIMPLE-NAT.. done.
8650
8651 %SIMPLE-NAT>
8652 \end{ccode}
8653 \end{vvtm}
8654 After \verb|open|ing, module elements and various commands can be input.
8655 The above score adds a new constant \verb|a| (to represent ``any''
8656 natural number) and an equation (for induction hypothesis), and invlokes
8657 reduction commands (base case and induction step). The system evaluates
8658 ``\verb|0 + 0|'' and ``\verb|s(a) + 0|'' as usual, and returns the results
8659 \verb|0| and \verb|s(a)|.
8660 \begin{vvtm}
8661 \begin{ccode}
8662 %SIMPLE-NAT> op a : -> Nat .
8663
8664 %SIMPLE-NAT> eq a + 0 = a .
8665
8666 %SIMPLE-NAT> reduce 0 + 0 .
8667 *
8668 -- reduce in % : 0 + 0
8669 0 : Zero
8670 (0.000 sec for parse, 1 rewrites(0.000 sec), 2 matches)
8671 %SIMPLE-NAT> reduce s(a) + 0 .
8672
8673 -- reduce in % : s(a) + 0
8674 s(a) : NzNat
8675 (0.000 sec for parse, 2 rewrites(0.000 sec), 4 matches)
8676 %SIMPLE-NAT> close
8677
8678 CafeOBJ>
8679 \end{ccode}
8680 \end{vvtm}
8681 You may have noticed quizzical ``\verb|*|'' in the above
8682 session. They are not print smudges.
8683 When a module is opened, the system processes the subsequent
8684 declarations and commands as if they were entered during a normal
8685 module declaration, and tries to tell you that it is working
8686 furiously (cf. an aside in Section \ref{sec:p2-comment-syntax}).
8687
8688 \begin{warning}
8689 As you may have noticed in the above example,
8690 the current implementation requires a blank-period terminator
8691 for each declaration (note ``\verb| .|'' after the operator
8692 declaration above).
8693 \end{warning}
8694
8695 The opened module and the current module need not be the same. For
8696 example, if the current module is \verb|NERD|, you can have a session
8697 like
8698 \begin{vvtm}
8699 \begin{ccode}
8700 NERD> open SIMPLE-NAT
8701
8702 -- opening module SIMPLE-NAT_.* done.
8703
8704 %SIMPLE-NAT> select BOOL
8705 ...
8706 BOOL> close
8707
8708 NERD>
8709 \end{ccode}
8710 \end{vvtm}
8711 By opening \verb|SIMPLE-NAT|, the temporary new module is set to be current,
8712 as indicated by the prompt. \verb|select| command switches the current
8713 module to \verb|BOOL|. \verb|close| restores the previous
8714 current module (remember that the effects of declarations and commands
8715 during opening are temporary).
8716 A convenient way to set the current module while it is
8717 opened is selecting the module ``\verb|%|''.
8718 \begin{vvtm}
8719 \begin{ccode}
8720 %SIMPLE-NAT> select %
8721
8722 %SIMPLE-NAT>
8723 \end{ccode}
8724 \end{vvtm}
8725
8726 \begin{warning}
8727 As you have guessed, the module name ``\verb|%|'' refers to the
8728 temporary module. You should not use ``\verb|%|'' as a module name.
8729 \end{warning}
8730
8731 \subsection{Constant On the Fly}\label{sec:p2-temporary-constant}
8732
8733 Let us think awhile of the example of the previous section. In
8734 the proof score, a constant \verb|a| was declared by
8735 \begin{vvtm}
8736 \begin{ccode}
8737 op a : -> Nat .
8738 \end{ccode}
8739 \end{vvtm}
8740 to represent {\em any} element of sort \verb|Nat|. Such declarations
8741 appear quite often when you are using the evaluation mechanism to
8742 prove theorems. To prove that \verb|0| is a right identity, \verb|a|
8743 appeared to calculate the induction step
8744 \begin{vvtm}
8745 \begin{ccode}
8746 reduce s(a) + 0 .
8747 \end{ccode}
8748 \end{vvtm}
8749 To make proofs more concise, you may use on-the-fly declarations
8750 of constants, similar to variable declarations on the fly
8751 (Section \ref{sec:p2-variable-decl}).
8752
8753 \begin{bsyntax} constant declaration on the fly \Hline
8754 \textit{constant\_name}``\texttt{:}''\textit{sort\_name}
8755 \end{bsyntax}
8756
8757 where a constant name is a character string that starts with
8758 the backquote ``\verb|`|''.
8759 The effects of declarations are temporary, like the case of variables.
8760 Using this construct, it is possible to invoke reduction commands,
8761 without \verb|open|ing modules, as
8762 \begin{vvtm}
8763 \begin{ccode}
8764
8765 CafeOBJ> select SIMPLE-NAT
8766
8767 SIMPLE-NAT> reduce 0 + 0 .
8768
8769 -- reduce in SIMPLE-NAT : 0 + 0
8770 0 : Zero
8771 (0.000 sec for parse, 1 rewrites(0.000 sec), 1 matches)
8772 SIMPLE-NAT> reduce s(`a:Nat) + 0 .
8773
8774 -- reduce in SIMPLE-NAT : s(`a:Nat) + 0
8775 s(`a:Nat + 0) : NzNat
8776 (0.000 sec for parse, 1 rewrites(0.000 sec), 6 matches)
8777 SIMPLE-NAT>
8778 \end{ccode}
8779 \end{vvtm}
8780 This time, the hypothesis is implicit: instead of stating and using
8781 it explicitly, you take note of it in analysing the result of the
8782 second evaluation.
8783
8784 \begin{warning}
8785 In fact, you get quite similar effects by using variables on the
8786 fly, instead of constants. Although evaluation is a procedure
8787 for ground terms, the system allows variables within terms to
8788 be reduced. Here is an example corresponding to the second reduction
8789 above.
8790 \begin{vvtm}
8791 \begin{ccode}
8792 SIMPLE-NAT> reduce s(X:Nat) + 0 .
8793
8794 -- reduce in SIMPLE-NAT : s(X:Nat) + 0
8795 s(X:Nat + 0) : NzNat
8796 (0.000 sec for parse, 1 rewrites(0.000 sec), 6 matches)
8797 SIMPLE-NAT>
8798 \end{ccode}
8799 \end{vvtm}
8800 But note that the system does not treat non-ground terms as such.
8801 It treats variables as if they were constants, and would not attempt
8802 unification.
8803 \end{warning}
8804
8805 \section{Applying Rewrite Rules}\label{sec:p2-applying-rules}
8806
8807 In Section \ref{sec:p2-open-close}, it was shown that reductions can be
8808 used to prove some theorems. For more demanding theorems, they are
8809 inadequate. Consider the additive group theory written in \cafeobj.
8810 \label{exs:group}
8811 \begin{vvtm}
8812 \begin{ccode}
8813 module* GROUP {
8814 [ G ]
8815 op 0 : -> G
8816 op _+_ : G G -> G { assoc }
8817 op -_ : G -> G
8818 var X : G
8819 eq 0 + X = X .
8820 eq (- X) + X = 0 .
8821 }
8822 \end{ccode}
8823 \end{vvtm}
8824 Suppose you want to show that ``\verb|- X|'' is also a right inverse
8825 (so {\em the} inverse). Using \verb|open|, you may write a score as
8826 \begin{vvtm}
8827 \begin{ccode}
8828 open GROUP
8829 op a : -> G .
8830 reduce a + (- a) .
8831 \end{ccode}
8832 \end{vvtm}
8833 or, using a constant on the fly, just
8834 \begin{vvtm}
8835 \begin{ccode}
8836 reduce in GROUP : `a:G + (- `a) .
8837 \end{ccode}
8838 \end{vvtm}
8839 But the result of reduction is simply
8840 \begin{vvtm}
8841 \begin{ccode}
8842 CafeOBJ> reduce in GROUP : `a:G + (- `a) .
8843
8844 -- reduce in GROUP : `a:G + - `a:G
8845 `a:G + - `a:G : G
8846 (0.000 sec for parse, 0 rewrites(0.000 sec), 8 matches)
8847 CafeOBJ>
8848 \end{ccode}
8849 \end{vvtm}
8850 which is no wonder, since no equation has a matching lefthand side.
8851 In fact, a standard proof goes as follows.
8852 \begin{vvtm}
8853 \begin{ccode}
8854 a + (- a) =
8855 0 + a + (- a) =
8856 (- (- a)) + (- a) + a + (- a) =
8857 (- (- a)) + 0 + (- a) =
8858 (- (- a)) + (- a) =
8859 0
8860 \end{ccode}
8861 \end{vvtm}
8862 where we omitted parentheses since ``\verb|_+_|'' was declared associative.
8863 This proof requires two ``reverse'' applications of rewrite rules.
8864
8865 \subsection{Start, Then Apply}\label{sec:p2-apply-command}
8866
8867 To accommodate such proofs as above, \cafeobj~supports the
8868 following set of commands.
8869
8870 \begin{bsyntax} \texttt{start} command \index{\texttt{start}} \Hline
8871 \texttt{start} \textit{term} ``\texttt{.}''
8872 \end{bsyntax}
8873
8874 where a term is a term of the opened module
8875 (Section \ref{sec:p2-open-close}), or in the current context
8876 (Section \ref{sec:p2-current-module}).
8877 This command sets the focus on the given term, which may be printed and
8878 referred to in the subsequent commands. \verb|show| command
8879 (Section \ref{sec:p2-show-command}) with \verb|term| as argument prints
8880 the term just selected.\index{\texttt{show}}\index{\texttt{term}}
8881
8882 \begin{bsyntax} \texttt{apply} command \index{\texttt{apply}} \Hline
8883 \texttt{apply} \textit{action} $[$\textit{substitution}$]$ \textit{range selection}
8884 \end{bsyntax}
8885
8886 where the action, substitution, range and selection are as follows.
8887
8888 \index{\texttt{reduce}}\index{\texttt{exec}}\index{\texttt{print}}
8889 \begin{tabular}{r c l}
8890 \textit{action} &::=& \verb|reduce| \\
8891 &$|$& \verb|exec| \\
8892 &$|$& \verb|print| \\
8893 &$|$& \textit{rewrite\_rule} \\
8894 \end{tabular}
8895
8896 where a rewrite rule is
8897
8898 \begin{bsyntax} rule specification in \texttt{}apply\textit{} command \Hline
8899 $[$``\texttt{+}''$|$``\texttt{-}''$]$$[$\textit{module\_name}$]$``\texttt{.}''$\{$ \textit{number}$|$\textit{label} $\}$
8900 \end{bsyntax}
8901
8902 If the action is \texttt{reduce} or \texttt{exec},
8903 the (sub)term specified by the range and
8904 selection is reduced. If it is \texttt{print}, the (sub)term is printed.
8905 If it is a rewrite rule, the chosen rule is applied, once, to the
8906 subterm. In the last case, the numbered or labelled rule of the
8907 module (if omitted, of the opened module) is used from left to right
8908 (if the sign is ``\verb|+|'' or none), or right to left (if ``\verb|-|'').
8909 We will first show a couple of examples in
8910 Section \ref{sec:p2-usage-of-apply} and
8911 say more on this construct later (Section \ref{sec:p2-apply-rule-id}).
8912
8913 Substitutions are of the form
8914
8915 \begin{bsyntax} substitution specification in \texttt{}apply\textit{} command \Hline
8916 ~~~~\texttt{}with\textit{} $\{$ variable ``\/\texttt{}=\textit{}'' term $\}$ $+_,$
8917 \end{bsyntax}
8918
8919 This option is necessary only when the action is a rewrite rule,
8920 and binds variables that appear in the rule (see the next section
8921 for why this option exists).
8922
8923 Ranges and selections are as follows.
8924
8925 \begin{tabular}{r c l}\index{\texttt{within}}\index{\texttt{at}}
8926 \textit{range} &::=& \verb|within| $|$ \verb|at|
8927 \end{tabular}
8928
8929 \verb|within| means at or inside the (sub)term specified by the
8930 selection, while \verb|at| means exactly at the (sub)term.
8931
8932 \begin{tabular}{r c l}\index{\texttt{of}}
8933 \textit{selection} &::=& \textit{selector} $\{$ \texttt{of} \textit{selector} $\}$ $*$\\
8934 \index{\texttt{top}}\index{\texttt{term}}\index{\texttt{subterm}}
8935 \textit{selector} &::=& \verb|top| \\
8936 &$|$& \verb|term| \\
8937 &$|$& \verb|subterm| \\
8938 &$|$& ``\verb|(|'' \textit{number\_list} ``\verb|)|'' \\
8939 &$|$& ``\verb|[|'' \textit{sublist} ``\verb|]|'' \\
8940 &$|$& ``\verb|{|'' \textit{subset} ``\verb|}|'' \\
8941 \end{tabular}
8942
8943 \verb|top| and \verb|term| mean the entire term.
8944 \verb|subterm| means the pre-chosen subterm (see the next section).
8945 For the other alternatives,
8946 \begin{itemize}
8947 \item
8948 The list of numbers, separated by blanks, within ``\verb|(|'', ``\verb|)|''
8949 indicates a subterm by tree search. For example, ``\verb|(2 1)|'' means
8950 the first argument of the second argument; if ``\verb|a + (b * c)|'' is
8951 the term of concern, it means \verb|b|.
8952 \item The
8953 \verb|[|, \verb|]| selector makes sense only with associative operators, and
8954 indicates a subterm in a flattened structure. For
8955 example, if ``\verb|_*_|'' was declared associative and given a term \newline
8956 ``\verb|a * b * c * d * e|'', ``\verb|[2 .. 4]|'' means ``\verb|b * c * d|'',
8957 while ``\verb|[2 .. 2]|'' means \verb|b|. The latter case can be
8958 abbreviated to \verb|[2]|.
8959 \item The
8960 \verb|{|, \verb|}| selector makes sense only with associative and commutative
8961 operators, and indicates a subterm in a multiset structure.
8962 For example, if ``\verb|_*_|'' was declared associative and commutative,
8963 and given a term ``\verb|b * c * d * e * f|'', both ``\verb|{2, 4}|'' and
8964 ``\verb|{4, 2}|'' means a subterm \newline
8965 ``\verb|c * e|'', while \verb|{4}| means
8966 \verb|e|.
8967 \end{itemize}
8968 Note that the above selections are indeed subterms.
8969 If ``\verb|_*_|'' is associative, the connectivity is immaterial, in
8970 whatever way the term was originally given. If it is commutative in
8971 addition, even the order is immaterial.
8972
8973 \begin{warning}
8974 \verb|top| is obsolete, and is here only for historical curiosity.
8975 \end{warning}
8976
8977 \subsection{Applying Apply Command}\label{sec:p2-usage-of-apply}
8978
8979 \verb|apply| command has a very complicated syntax. It is better to
8980 explain its behaviours by examples.
8981 We use the group theory presented at the start of this section.
8982
8983 To show ``\verb|- X|'' is a right inverse, you may use the
8984 following sequence of commands.
8985 \begin{vvtm}
8986 \begin{ccode}
8987 open GROUP
8988 op a : -> G .
8989 start a + (- a) .
8990 apply -.1 at (1) .
8991 apply -.2 with X = - a at [1] .
8992 apply reduce at term .
8993 \end{ccode}
8994 \end{vvtm}
8995 (The numbers of rewrite rules are printed by \verb|show| command. See
8996 Section \ref{sec:p2-apply-rule-id}.)
8997 After two applications of reversed rules, reduction yields the desired
8998 result, as follows.
8999 \begin{vvtm}
9000 \begin{ccode}
9001 NAT> open GROUP
9002
9003 -- opening module GROUP... done.
9004
9005 %GROUP> op a : -> G
9006
9007 %GROUP> start a + (- a) .
9008
9009 %GROUP> apply -.1 at (1) .
9010 *result 0 + a + - a : G
9011
9012 %GROUP> apply -.2 with X = - a at [1] .
9013 result - (- a) + - a + a + - a : G
9014
9015 %GROUP> apply reduce at term .
9016 result 0 : G
9017
9018 %GROUP>
9019 \end{ccode}
9020 \end{vvtm}
9021 Look in more detail what happened at the second \verb|apply|
9022 command. This command applied
9023 \begin{vvtm}
9024 \begin{ccode}
9025 eq (- X) + X = 0 .
9026 \end{ccode}
9027 \end{vvtm}
9028 to the subterm \verb|0|. Without saying anything, a variable \verb|X|
9029 would be introduced. This situation often occurs when a rule is
9030 reversely applied. The substitution ``\verb|X = - a|'' avoids that.
9031
9032 You can continue, and prove another theorem that \verb|0| is a right identity
9033 (so {\em the} identity).
9034 Since we already showed ``\verb|a + (- a)|'' equals \verb|0|, it is
9035 sound to add the equation. That leads to the command sequence
9036 \begin{vvtm}
9037 \begin{ccode}
9038 eq a + (- a) = 0 .
9039 start a + 0 .
9040 apply -.2 with X = a at (2) .
9041 apply reduce at term .
9042 \end{ccode}
9043 \end{vvtm}
9044 which mirrors the reasoning
9045 \begin{vvtm}
9046 \begin{ccode}
9047 a + 0 = a + (- a) + a = 0 + a = a
9048 \end{ccode}
9049 \end{vvtm}
9050
9051 \subsection{Choosing Subterms}\label{sec:p2-choose-command}
9052
9053 If a term has a complicated structure, it is error-prone to
9054 select a subterm at once. To make safer selections, a command
9055 is supported.
9056
9057 \begin{bsyntax} \texttt{choose} command \index{\texttt{choose}}\Hline
9058 \texttt{choose} \textit{selection}
9059 \end{bsyntax}
9060
9061 where a selection is as in \verb|apply|
9062 (Section \ref{sec:p2-apply-command}). This command
9063 sets the focus to the chosen subterm, and its successive application
9064 narrows the focus gradually.
9065 \verb|show| command with \verb|subterm| argument may be used to print
9066 the current subterm in focus.\index{\texttt{show}}\index{\texttt{subterm}}
9067
9068 For example, if you define a module of rings as\label{exs:ring}
9069 \begin{vvtm}
9070 \begin{ccode}
9071 module* RING {
9072 [ R ]
9073 op 0 : -> R
9074 op _+_ : R R -> R { assoc comm prec: 35 }
9075 op -_ : R -> R
9076 op _*_ : R R -> R { assoc prec: 31 }
9077 vars X Y Z : R
9078 eq 0 + X = X .
9079 eq (- X) + X = 0 .
9080 eq X * (Y + Z) = X * Y + X * Z .
9081 eq (Y + Z) * X = Y * X + Z * X .
9082 }
9083 \end{ccode}
9084 \end{vvtm}
9085 and after \verb|open|ing \verb|RING|, you get
9086 \begin{vvtm}
9087 \begin{ccode}
9088 %RING> op f_ : R -> R .
9089
9090 %RING> start ((f f 0) * ((f f f 0) + (- (f f f f 0)))) *
9091 ((- (f f f f 0)) + ((f f f 0) * ((f f f f 0) *
9092 ((f f 0) + ((f f f f f 0) * (f 0)))))) .
9093
9094 %RING> choose (2) .
9095
9096 %RING> show subterm
9097 ((- (f (f (f (f 0))))) + ((f (f (f 0))) * ((f (f (f (f 0)))) * ((f (f 0)) +
9098 ((f (f (f (f (f 0))))) * (f 0)))))) : R
9099
9100 %RING> choose (2) .
9101
9102 %RING> show subterm
9103 ((f (f (f 0))) * ((f (f (f (f 0)))) * ((f (f 0)) + ((f (f (f (f (f
9104 0))))) * (f 0))))) : R
9105
9106 %RING> choose [1 .. 2] .
9107
9108 %RING> show subterm
9109 ((f (f (f 0))) * (f (f (f (f 0))))) : R
9110
9111 %RING> show term
9112 (((f (f 0)) * ((f (f (f 0))) + (- (f (f (f (f 0))))))) * ((- (f (f
9113 (f (f 0))))) + (((f (f (f 0))) * (f (f (f (f 0))))) * ((f (f
9114 0)) + ((f (f (f (f (f 0))))) * (f 0)))))) : R
9115
9116 %RING>
9117 \end{ccode}
9118 \end{vvtm}
9119 The last command was used to print the entire term that had been
9120 \verb|start|ed. This command was
9121 also useful in the stepper (cf. Section \ref{sec:p2-eval-step-mode}).
9122 You may also print the subterm in a tree form, as
9123 \begin{vvtm}
9124 \begin{ccode}
9125 %RING> show subterm tree
9126 ((f (f (f 0))) * (f (f (f (f 0))))) : R
9127 _*_
9128 / \
9129 f_ f_
9130 | |
9131 f_ f_
9132 | |
9133 f_ f_
9134 | |
9135 0 f_
9136 |
9137 0
9138
9139
9140
9141 %RING>
9142 \end{ccode}
9143 \end{vvtm}
9144 The current term, subterm etc. can also be printed by \verb|show|
9145 command, as\index{\texttt{show}}\index{\texttt{context}}
9146 (cf. Sections \ref{sec:p2-current-module}, \ref{sec:p2-apply-to-cond})
9147 \begin{vvtm}
9148 \begin{ccode}
9149 %RING> show context
9150 -- current context :
9151 [module] %
9152 [special bindings]
9153 $$term = (((f (f 0)) * ((f (f (f 0))) + (- (f (f (f (f 0)))))))
9154 * ((- (f (f (f (f 0))))) + (((f (f (f 0))) * (f (f (f (f 0)))))
9155 * ((f (f 0)) + ((f (f (f (f (f 0))))) * (f 0)))))) : R
9156 $$subterm = ((f (f (f 0))) * (f (f (f (f 0))))) : R
9157 [bindings] empty.
9158 [selections]
9159 1| [ 1 .. 2 ] .
9160 2| ( 2 ) .
9161 3| ( 2 ) .
9162 [pending actions] none.
9163 [stop pattern] not specified.
9164
9165 %RING>
9166 \end{ccode}
9167 \end{vvtm}
9168
9169 \subsection{Identifying Rewrite Rules}\label{sec:p2-apply-rule-id}
9170
9171 We have not shown how to obtain the number that designates a specific
9172 rewrite rule. Returning to \verb|GROUP|,
9173 you may print rewrite rules --- or axioms so regarded
9174 --- as follows.\index{\texttt{show}}\index{\texttt{rules}}
9175 \begin{vvtm}
9176 \begin{ccode}
9177 CafeOBJ> open GROUP
9178
9179 -- opening module GROUP_.* done.
9180
9181 %GROUP> show rules
9182 -- rewrite rules in module : %GROUP
9183 1 : eq 0 + X = X
9184 2 : eq - X + X = 0
9185
9186 %GROUP>
9187 \end{ccode}
9188 \end{vvtm}
9189 Rules of any module may be printed, by supplying an extra argument.
9190 \begin{vvtm}
9191 \begin{ccode}
9192 %GROUP> show rules RING
9193 -- rewrite rules in module : RING
9194 1 : eq 0 + X = X
9195 2 : eq - X + X = 0
9196 3 : eq X * (Y + Z) = X * Y + X * Z
9197 4 : eq (Y + Z) * X = Y * X + Z * X
9198
9199 %GROUP>
9200 \end{ccode}
9201 \end{vvtm}
9202 It is also possible to print all the rules, including those declared
9203 in imported modules, by specifying \verb|all|, as in\index{\texttt{all}}
9204 \begin{vvtm}
9205 \begin{ccode}
9206 %GROUP> show all rules
9207 \end{ccode}
9208 \end{vvtm}
9209
9210 The numbering of the above is used to specify rewrite rules.
9211 For example, you encountered in Section \ref{sec:p2-usage-of-apply} a
9212 command
9213 \begin{vvtm}
9214 \begin{ccode}
9215 %GROUP> apply -.1 at (1) .
9216 \end{ccode}
9217 \end{vvtm}
9218 The specified rule, according to the output above, was
9219 \begin{vvtm}
9220 \begin{ccode}
9221 1 : eq 0 + X = X
9222 \end{ccode}
9223 \end{vvtm}
9224 and the minus sign was added to apply this rule in reverse.
9225 You may decorate a rule number with a module name, like
9226 \begin{vvtm}
9227 \begin{ccode}
9228 %GROUP> apply -GROUP.1 at (1) .
9229 \end{ccode}
9230 \end{vvtm}
9231 which will apply the same rule as before.
9232 Axiom labels may also be used to designate rules. For example, if
9233 the above equation were labelled as
9234 \begin{vvtm}
9235 \begin{ccode}
9236 eq [l-id] : 0 + X = X .
9237 \end{ccode}
9238 \end{vvtm}
9239 the \verb|apply| command might as well be
9240 \begin{vvtm}
9241 \begin{ccode}
9242 %GROUP> apply -.l-id at (1) .
9243 \end{ccode}
9244 \end{vvtm}
9245
9246 \begin{warning}
9247 Do not put blanks anywhere in between the sign, module name, period,
9248 number or label. Memorise ``a rule spec hates vacuum''.
9249 \end{warning}
9250
9251 Finally, \verb|show| command with \verb|rule| (not \verb|rules|) may be
9252 used to print a single rule. This command is useful when the number of
9253 rewrite rules is large. An example:\index{\texttt{show}}\index{\texttt{rule}}
9254 \begin{vvtm}
9255 \begin{ccode}
9256 %GROUP> show rule .2
9257 rule 2 of the last module
9258 - X + X = 0
9259 (This rule rewrites up.)
9260 \end{ccode}
9261 \end{vvtm}
9262 Do not forget the period before \verb|2|. Arguments to this command are
9263 rule specifications, and ``\verb|.2|'' is of rule specification form,
9264 according to the definition in Section \ref{sec:p2-apply-command}.
9265
9266 ``\verb|show rule|'' and ``\verb|show rules|'' may be invoked when no module
9267 is opened. In such a case, the rule(s) of the current module are printed.
9268 \subsection{Appyling Conditionals}\label{sec:p2-apply-to-cond}
9269
9270 When the action is a rewrite rule, \verb|apply| command tries to
9271 apply just one rule and immediately returns the control to you.
9272 What if the rule is conditional? Recall \verb|GCD| (Section
9273 \ref{exs:gcd}):
9274 \begin{vvtm}
9275 \begin{ccode}
9276 CafeOBJ> open GCD
9277
9278 -- opening module GCD.. done.
9279
9280 %GCD> start gcd(s(0),s(s(0))) .
9281
9282 %GCD> show rules
9283 -- rewrite rules in module : %GCD
9284 1 : ceq gcd(N,M) = gcd(M,N) if N < M
9285 2 : eq gcd(N,0) = N
9286 3 : ceq gcd(s(N),s(M)) = gcd(s(N) - s(M),s(M)) if not N < M
9287
9288 %GCD> apply +.1 at term .
9289 shifting focus to condition
9290 -- condition(1) s(0) < s(s(0)) : Bool
9291
9292 %GCD> show context
9293 -- current context :
9294 [module] %
9295 [special bindings]
9296 $$term = (s(0) < s(s(0))) : Bool
9297 $$subterm = $$term
9298 [bindings] empty.
9299 [selections] empty.
9300 [pending actions]
9301 1| in gcd(s(0),s(s(0))) at top
9302 | rule ceq gcd(N,M) = gcd(M,N) if N < M
9303 | condition s(0) < s(s(0)) replacement gcd(s(s(0)),s(0))
9304 [stop pattern] not specified.
9305
9306 %GCD>
9307 \end{ccode}
9308 \end{vvtm}
9309 It looked certain that the rule \verb|1| would apply, so you tried it.
9310 The system then told that, since that rule was conditional, it had to
9311 resolve the condition first.
9312
9313 \verb|show| command with \index{\texttt{context}} argument were
9314 partly explained in Sections \ref{sec:p2-current-module} and
9315 \ref{sec:p2-choose-command}. What was remained was the misterious
9316 heading ``[pending actions]''. This part is used exactly in
9317 a situation like now, and contains terms to be further
9318 reduced after a condition --- the current focus --- has been evaluated.
9319
9320 The instance of the condition requires
9321 rules in \verb|SIMPLE-NAT+| (Section \ref{exs:simple-nat+}),
9322 so you have to continue as follows.
9323 \begin{vvtm}
9324 \begin{ccode}
9325 %GCD> show rules SIMPLE-NAT+
9326 -- rewrite rules in module : SIMPLE-NAT+
9327 1 : eq 0 - M = 0
9328 2 : eq N - 0 = N
9329 3 : eq s(N) - s(M) = N - M
9330 4 : eq 0 < s(N) = true
9331 5 : eq N < 0 = false
9332 6 : eq s(N) < s(M) = N < M
9333
9334 %GCD> apply +SIMPLE-NAT+.6 at term .
9335 -- condition(1) 0 < s(0) : Bool
9336
9337 %GCD> apply +SIMPLE-NAT+.4 at term .
9338 -- condition(1) true : Bool
9339 -- condition is satisfied, applying rule
9340 -- shifiting focus back to previous context
9341 result gcd(s(s(0)),s(0)) : Nat
9342
9343 %GCD>
9344 \end{ccode}
9345 \end{vvtm}
9346 At last the condition was satisfied, and the system rewrote the
9347 original term. You may continue, this time applying the rule \verb|3|.
9348 \begin{vvtm}
9349 \begin{ccode}
9350 %GCD> apply +.3 at term .
9351 shifting focus to condition
9352 -- condition(1) not s(0) < 0 : Bool
9353
9354 %GCD>
9355 \end{ccode}
9356 \end{vvtm}
9357 But reduction of ``\verb|_<_|'' is obvious so you apply \verb|reduce|
9358 at once.
9359 \begin{vvtm}
9360 \begin{ccode}
9361 %GCD> apply reduce at term .
9362 -- condition(1) true : Bool
9363 -- condition is satisfied, applying rule
9364 -- shifiting focus back to previous context
9365 result gcd(s(s(0)) - s(0),s(0)) : Nat
9366
9367 %GCD>
9368 \end{ccode}
9369 \end{vvtm}
9370 The original term was further rewritten, and you can continue as before.
9371
9372 As shown by this frustrating example, what \verb|apply| basically
9373 offers is a painful step-by-step reasoing. There are ways to skip
9374 certain parts at one go. One way is to invoke the action \verb|reduce|,
9375 as above. In the case of conditional rewrite rules, there is another,
9376 systematic way to do it. Let us redo the above session using this
9377 mechanism.\index{\texttt{reduce}}\index{\texttt{conditions}}
9378 \begin{vvtm}
9379 \begin{ccode}
9380 %GCD> start gcd(s(0), s(s(0))) .
9381
9382 %GCD> set reduce conditions on
9383
9384 %GCD> apply +.1 at term .
9385 result gcd(s(s(0)),s(0)) : Nat
9386
9387 %GCD> apply +.3 at term .
9388 result gcd(s(s(0)) - s(0),s(0)) : Nat
9389
9390 %GCD> apply +.1 at term .
9391
9392 [Warning]: rule not applied
9393 result gcd(s(s(0)) - s(0),s(0)) : Nat
9394
9395 %GCD>
9396 \end{ccode}
9397 \end{vvtm}
9398 The switch ``\verb|reduce conditions|'', when \verb|on|, makes the
9399 system to evaluate conditions automatically.
9400
9401 \section{Matching Terms}\label{sec:p2-matching-terms}
9402
9403 Matching is a basic procedure in evaluation (Section \ref{sec:p2-trs-basic}).
9404 It may be useful by itself as a theorem-proving helper, since
9405 \begin{itemize}
9406 \item When you use \verb|reduce| command, it is a good practice to write
9407 down the expected result beforehand. If the actual result differs, you
9408 immediately notice something is going wrong. A standard way to realise
9409 this practice is to use a printable comment ``\verb|-->|'' or ``\verb|**>|''
9410 (Section \ref{sec:p2-comment-syntax}), as
9411 \begin{vvtm}
9412 \begin{ccode}
9413 reduce s(s(0)) + s(0) .
9414 **> should be: s(s(s(0)))
9415 \end{ccode}
9416 \end{vvtm}
9417 The system then prints
9418 \begin{vvtm}
9419 \begin{ccode}
9420 -- reduce in SIMPLE-NAT : s(s(0)) + s(0)
9421 s(s(s(0))) : NzNat
9422 (0.000 sec for parse, 3 rewrites(0.000 sec), 5 matches)
9423 **> should be: s(s(s(0)))
9424 \end{ccode}
9425 \end{vvtm}
9426 so that you can compare the actual and expected results easily.
9427 A more sophisticated mechanism is to invoke a matching
9428 procedure. And
9429 \item In using \verb|reduction| and \verb|apply| commands for
9430 theorem proving, it is necessary to know what rewrite rules are
9431 applicable to what terms. You can inspect the rules by \verb|show|
9432 command, but if the set of rules is large, it is tedious.
9433 A tool to list applicable rules is helpful. ``Applicable'' here
9434 means ``having matchable lefthand side''.
9435 \end{itemize}
9436
9437 \subsection{Match Command}\label{sec:p2-match-command}
9438
9439 \begin{bsyntax} \texttt{match} command \index{\texttt{match}}\Hline
9440 \texttt{match} \textit{term\_specifier} \texttt{to} \textit{pattern} ``\texttt{.}''
9441 \end{bsyntax}
9442
9443 The last period (preceded by a blank) is necessary. Term specifiers and
9444 patterns are as follows.
9445
9446 \begin{tabular}{r c l}\index{\texttt{top}}\index{\texttt{term}}\index{\texttt{it}}
9447 \index{\texttt{subterm}}
9448 \textit{term\_specifier} &::=& \verb|top| \\
9449 &$|$& \verb|term| \\
9450 &$|$& \verb|it| \\
9451 &$|$& \verb|subterm| \\
9452 &$|$& \textit{term} \\
9453 \end{tabular}
9454
9455 where a term is a usual term. \verb|top| or the literal \verb|term|
9456 means the term set by
9457 \verb|start| command; \verb|subterm| means the term selected by \verb|choose|
9458 commands; \verb|it| is the same as \verb|subterm| if \verb|choose| was
9459 used, and is the same as \verb|top| otherwise.
9460
9461 \begin{warning}
9462 Unlike former versions, the current version do not require enclosing
9463 parentheses for a term. If you are cautious, put them anyway. They
9464 are harmless.
9465 \end{warning}
9466
9467 \begin{tabular}{r c l}\index{\texttt{all}}\index{\texttt{rules}}\index{\texttt{+rules}}
9468 \index{\texttt{-rules}}
9469 \textit{pattern} &::=& $[$ \verb|all| $]$
9470 $\{$ \verb|rules| $|$ \verb|+rules| $|$ \verb|-rules| $\}$ \\
9471 &$|$& \textit{term}
9472 \end{tabular}
9473
9474 where a term is a usual term. \verb|rules|, ``\verb|+rules|'' or
9475 ``\verb|-rules|''
9476 lists rewrite rules applicable to the specified term. ``\verb|+rules|'' means
9477 the rules with matching lefthand sides, ``\verb|-rules|'' means
9478 those with
9479 matching righthand sides, and \verb|rules| means both.
9480 If optional \verb|all| is given, all the rules in the current context,
9481 including those declared in built-in modules, are inspected.
9482
9483 If a pattern is a term, matching is attempted, and if successful,
9484 the matching subsitution is printed. Is a pattern is \verb|rules|,
9485 ``\verb|+rules|'' or ``\verb|-rules|'',
9486 the list of applicable rules, together with
9487 corresponding matching substutitions, are printed.
9488
9489 \subsection{Matching Terms to Terms}\label{sec:p2-match-to-term}
9490
9491 We use \verb|GROUP| (Section \ref{exs:group}) for examples.
9492 \begin{vvtm}
9493 \begin{ccode}
9494 CafeOBJ> select GROUP
9495
9496 GROUP> match 0 + 0 to X:G + 0 .
9497 -- match success.
9498 substitution : { X |-> 0 }
9499 -- No more match
9500
9501 GROUP> match 0 + 0 to X:G + Y:G .
9502 -- match success.
9503 substitution : { X |-> 0, Y:G |-> 0 }
9504 -- No more match
9505
9506 GROUP> match 0 to 0 + 0 .
9507 -- no match
9508
9509 GROUP>
9510 \end{ccode}
9511 \end{vvtm}
9512 As shown above, the system prints the substitution when
9513 matching succeeds.
9514
9515 The system recognises equational theory
9516 attributes, such as associativity (Section \ref{sec:p2-equational-theory}),
9517 and tries all the possibilities, as
9518 \begin{vvtm}
9519 \begin{ccode}
9520 GROUP> match 0 + 0 + 0 to X:G + Y:G .
9521 -- match success.
9522 substitution : { X |-> 0, Y:G |-> 0 + 0 }
9523 >> More? [y/n] : y
9524 -- match success :
9525 * substitution : { X |-> 0 + 0, Y:G |-> 0 }
9526
9527 -- No more match
9528
9529 GROUP>
9530 \end{ccode}
9531 \end{vvtm}
9532 Remember that ``\verb|_+_|'' was declared associative. Modulo associativity,
9533 there are two ways of matching the above two terms.
9534
9535 \begin{warning}
9536 It is possible for a term to contain variables, both as a term specifier
9537 and as a pattern. For example, it is legal to try
9538 \begin{vvtm}
9539 \begin{ccode}
9540 match X:G + 0 to 0 + X':G .
9541 \end{ccode}
9542 \end{vvtm}
9543 But this does not acts as expected:
9544 \begin{vvtm}
9545 \begin{ccode}
9546 GROUP> match X:G + 0 to 0 + X':G .
9547 -- no match
9548
9549 GROUP>
9550 \end{ccode}
9551 \end{vvtm}
9552 \verb|match| is a matching procedure,
9553 not a unification. If a term (to the left of ``\verb|to|'') contains
9554 a variable, it is treated as a pseudo-constant. In the above example,
9555 \verb|X| acts as a constant, and cannot be substituted by \verb|0|.
9556 \end{warning}
9557
9558 \subsection{Matching Terms to Pattern}\label{sec:p2-match-to-pattern}
9559
9560 We next deal with the cases when the pattern designates a set of rules.
9561 For illustration, we add several equations before invoking matching.
9562 \begin{vvtm}
9563 \begin{ccode}
9564 GROUP> open GROUP
9565
9566 -- opening module GROUP_.* done.
9567
9568 %GROUP> ops a b : -> G .
9569
9570 %GROUP> eq X:G + Y:G = Y + X .
9571
9572 %GROUP> eq a + X:G = a + X + a .
9573
9574 %GROUP> start 0 + a + a .
9575
9576 %GROUP> match term to +rules .
9577 == matching rules to term : (0 + (a + a))
9578 +%.1 is eq 0 + X = X
9579 substitution : { X |-> a + a }
9580 +%.3 is eq X + Y:G = Y:G + X
9581 substitution : { Y:G |-> a + a, X |-> 0 }
9582 +%.4 is eq a + X = a + X + a
9583 substitution : { X |-> a }
9584
9585 %GROUP>
9586 \end{ccode}
9587 \end{vvtm}
9588 The system lists applicable rules to the term ``\verb|0 + a + a|''.
9589 In this output, a rule is numbered, adorned with the module name
9590 ``\verb|%|'' (the opened module: cf. Section \ref{sec:p2-usage-of-open}),
9591 and a sign. Remember that this form appears in arguments to \verb|apply|
9592 command (Section \ref{sec:p2-apply-rule-id}). You may have noticed
9593 two differences from the cases of matching terms to terms.
9594 \begin{itemize}
9595 \item Not all possibilities are considered. In the above output,
9596 only one matching possibility is shown for the rule \verb|3|, even though
9597 there are two possibilities (see the previous section).
9598 \item All the subterms of the given term is under consideration.
9599 The rule \verb|4| does not match the whole term, but does match
9600 a subterm.
9601 \end{itemize}
9602 The case where ``\verb|-rules|'' is specified is similar.
9603 \begin{vvtm}
9604 \begin{ccode}
9605 %GROUP> match term to -rules .
9606 == matching rules to term : (0 + (a + a))
9607 -%.1 is eq 0 + X = X
9608 substitution : { X |-> 0 + a + a }
9609 -%.3 is eq X + Y:G = Y:G + X
9610 substitution : { Y:G |-> 0, X |-> a + a }
9611 -%.20 is eq if true then CXU else CYU fi = CXU
9612 substitution : { CXU |-> 0 + a + a }
9613 -%.21 is eq if false then CXU else CYU fi = CYU
9614 substitution : { CYU |-> 0 + a + a }
9615
9616 %GROUP>
9617 \end{ccode}
9618 \end{vvtm}
9619
9620 \begin{warning}
9621 The last two printed rules are of \verb|BOOL|
9622 (actually, a submodule thereof). This rule is always applicable in
9623 reverse, since the righthand side is a single variable.
9624 \end{warning}
9625
9626 \chapter{Proving Module Properties}\label{sec:p2-semantic-prop}
9627
9628 \section{Check Command}\label{sec:p2-check-command}
9629
9630 Some of the module properties are checkable syntactically, within
9631 reasonable computation cost.
9632
9633 \begin{bsyntax} \texttt{check} command \index{\texttt{check}}\Hline
9634 \index{\texttt{regularity}}\index{\texttt{compatibility}}\index{\texttt{laziness}}
9635 \texttt{check} $\{$ \texttt{regularity} $|$ \texttt{compatibility} $\}$ $[$\textit{module\_expression}$]$
9636 \texttt{check} $\{$ \texttt{laziness} $\}$ $[$\textit{operator\_name}$]$
9637 \end{bsyntax}
9638
9639 A module expression may be omitted if the current module
9640 (Section \ref{sec:p2-current-module}) is set.
9641 \verb|regularity| may be abbreviated to \verb|reg| or \verb|regular|,
9642 \verb|compatibility| to \verb|compat|, and \verb|laziness| to \verb|lazy|.
9643
9644 \paragraph{Regularity}
9645
9646 If the first argument is \verb|regularity|, the system checks
9647 if the module is regular\index{regular}.
9648 Using \verb|INTB'| in Section \ref{exs:intb'}, you can
9649 obtain the following message.
9650 \begin{vvtm}
9651 \begin{ccode}
9652 CafeOBJ> check regularity INTB'
9653
9654 >> start regularity check ...
9655 >> The following sorts may be required for regularity:
9656 [ Nat^NonPos < NonPos Nat ]
9657 >> The following operators may be required for regularity:
9658 _+_ : Nat^NonPos Nat^NonPos -> Nat^NonPos
9659 CafeOBJ>
9660 \end{ccode}
9661 \end{vvtm}
9662 while with \verb|SIMPLE-NAT|, the system reports nothing, as
9663 \begin{vvtm}
9664 \begin{ccode}
9665 CafeOBJ> check regularity SIMPLE-NAT
9666
9667 >> start regularity check ...
9668 -- signature of module SIMPLE-NAT is regular.
9669 CafeOBJ>
9670 \end{ccode}
9671 \end{vvtm}
9672 Unlike \verb|regularize| command (Section \ref{sec:p2-wff-terms}),
9673 this command does not modify the signature.
9674
9675 \paragraph{Compatibility}
9676
9677 If the first argument is \verb|compatibility|, the system checks
9678 if the TRS defined by the module is compatible\index{compatible}, i.e.,
9679 every application of every rewrite rule to every well-formed term
9680 results in a well-formed term. Note that, in order-sorted
9681 rewriting, an application of a rewrite rule may introduce
9682 an ill-formed term. For (a contrived) example,
9683 \begin{vvtm}
9684 \begin{ccode}
9685 module NONCOMPAT {
9686 [ S < S' ]
9687 op a : -> S
9688 op b : -> S'
9689 op f : S -> S
9690 eq a = b .
9691 }
9692 \end{ccode}
9693 \end{vvtm}
9694 is not compatible, since \verb|f(a)|, which is well-formed, rewrites to
9695 \verb|f(b)|, which is ill-formed. As this example shows, non-compatibility
9696 is caused by a rewrite rule that ``raise''s the sort of a term. This is
9697 not a necessary condition of compatibility, however. In the absence of
9698 the operator \verb|f|, the above module {\em is} compatible.
9699 The system checks a necessary and sufficient condition of compatibility,
9700 as
9701 \begin{vvtm}
9702 \begin{ccode}
9703 CafeOBJ> check compatibility NONCOMPAT
9704
9705 >> started compatibility check:
9706
9707 >> module (corresponding TRS) is NOT compatible:
9708 - rewrite rule
9709 a = b
9710 violates the compatibility,
9711 and following operator(s) can possibly be affected:
9712 f : S -> S
9713 CafeOBJ> module COMPAT {
9714 [ S < S' ]
9715 op a : -> S
9716 op b : -> S'
9717 eq a = b .
9718 }
9719
9720 -- defining module COMPAT..._.* done.
9721 CafeOBJ> check compatibility COMPAT
9722
9723 >> started compatibility check:
9724
9725 >> module is compatible.
9726 CafeOBJ>
9727 \end{ccode}
9728 \end{vvtm}
9729
9730 \begin{warning}
9731 As shown in Section \ref{sec:p2-flexible-typing}, the current
9732 implementation regards
9733 a term like \verb|f(b)| above as {\em well-formed}, although of
9734 a questionable sort. The above definition should be rephrased, in this
9735 setting, in terms of error sorts.
9736 \end{warning}
9737
9738 \paragraph{Laziness}
9739
9740 If the first argument is \verb|laziness|,
9741 the system checks if the given operator can be evaluated lazily.
9742 The operator must be in the current module (which must be set
9743 beforehand). If an operator name is omitted, all the operators
9744 directly declared in the current module is checked.
9745
9746 Laziness\index{laziness} is related to evaluation strategies.
9747 The system checks a sufficient
9748 condition under which an operator may be evaluated lazily,
9749 as explained Section \ref{sec:p2-rew-strategy}. Here is an example.
9750 \begin{vvtm}
9751 \begin{ccode}
9752 CafeOBJ> module LAZY {
9753 [ S ]
9754 op f : S -> S
9755 op g : S -> S
9756 eq f(f(X:S)) = g(X) .
9757 eq g(X:S) = X .
9758 }
9759
9760 -- defining module LAZY..._..* done.
9761 CafeOBJ> select LAZY
9762
9763 LAZY> check laziness
9764
9765 ------------------------------------------------------------
9766 * laziness of operator: (f) *
9767 ------------------------------------------------------------
9768 f : S -> S
9769 * strict on the arguments : 1
9770 - rewrite strategy: 1 0
9771 - axioms:
9772 eq f(f(X:S)) = g(X:S)
9773
9774 ------------------------------------------------------------
9775 * laziness of operator: (g) *
9776 ------------------------------------------------------------
9777 g : S -> S
9778 * may delay the evaluation on the arguments : 1
9779 - rewrite strategy: 0 1 0
9780 - axioms:
9781 eq g(X:S) = X:S
9782
9783 LAZY>
9784 \end{ccode}
9785 \end{vvtm}
9786
9787 \begin{warning}
9788 The system only checks an apparent sufficient condition, and
9789 do not use a detailed strictness analysis as used in
9790 functional language compilers. The word ``laziness'' was chosen
9791 humbly and carefully.
9792 \end{warning}
9793
9794 \section{Theorem Proving Techniques}\label{sec:p2-theorem-proving-exs}
9795
9796 We have already shown a couple of proofs based on the TRS engine of
9797 \cafeobj(Sections \ref{sec:p2-usage-of-open}, \ref{sec:p2-usage-of-apply}).
9798 There a proof score was a sequence of commands, including
9799 \verb|open|, \verb|reduce| and \verb|apply|. The system reads the commands,
9800 rewrites terms as commanded, and prints the result. Behind such
9801 rewrite sequences is equational reasoning: schematically, the reasoning
9802 is of the form
9803 \begin{center}
9804 $t_0$ $=$ $t_1$ $=$ $\ldots$ $=$ $t_n$
9805 \end{center}
9806 and the theorem to prove is the equation $t_0$ $=$ $t_n$.
9807
9808 Another way to prove theorems by evaluation is to state the theorems
9809 as terms of sort \verb|Bool|, and to evaluate them. If the terms are
9810 reduced to \verb|true|, the theorems are proved. For example, to
9811 show
9812 \begin{vvtm}
9813 \begin{ccode}
9814 eq a + (- a) = 0 .
9815 \end{ccode}
9816 \end{vvtm}
9817 we may try to reduce the term
9818 \begin{vvtm}
9819 \begin{ccode}
9820 a + (- a) == 0
9821 \end{ccode}
9822 \end{vvtm}
9823 to \verb|true|. This approach does not confer
9824 you with much extra power. As explained in Section
9825 \ref{sec:p2-equality-pred}, in evaluating ``\verb|_==_|'',
9826 \cafeobj~simply reduces both arguments, and check if they are equal.
9827 Apart from the equality checking part, which involves
9828 equational theory (Section \ref{sec:p2-equational-theory}),
9829 what can be done with ``\verb|_==_|'' can be done without.
9830 In terms of presentation, however, this approach produces
9831 something like
9832 \begin{vvtm}
9833 \begin{ccode}
9834 ...
9835 %GROUP> start a + (- a) == 0 .
9836 ...
9837 %GROUP> apply reduce at term .
9838 result true : Bool
9839 \end{ccode}
9840 \end{vvtm}
9841 which is more appealing to intuition than
9842 \begin{vvtm}
9843 \begin{ccode}
9844 ...
9845 %GROUP> start a + (- a) .
9846 ...
9847 %GROUP> apply reduce at term .
9848 result 0 : G
9849 \end{ccode}
9850 \end{vvtm}
9851 where you must mentally compare the actual result with the expected.
9852 Especially, when both sides of the equation need evaluation, this
9853 approach provides you with a much more compact proof.
9854
9855 \subsection{Structural Induction}\label{sec:p2-proof-by-induction}
9856
9857 In Section \ref{sec:p2-usage-of-open}, we have presented a
9858 proof score to show that \verb|0| is a right identity of ``\verb|_+_|''
9859 under the definition of \verb|SIMPLE-NAT|. The score relies on
9860 structural induction\index{structural induction}.
9861
9862 Strictly speaking, proofs by structural induction only demonstrate
9863 that certain properties (called
9864 {\em inductive properties}\index{inductive property}) hold for
9865 the term-generated models of a module, including its initial model.
9866 Recall that ``\verb|module!|'' denotes such a
9867 model, while \verb|module| has both tight and loose denotations.
9868 Since \verb|SIMPLE-NAT| was declared with \verb|module|, what was proven
9869 in Section \ref{sec:p2-usage-of-open} is a property that does not
9870 necessarily hold when \verb|SIMPLE-NAT| denotes loose models.
9871 With this proviso, structural induction is certainly a useful
9872 technique, and you are often powerless without it.
9873
9874 Let us show a couple of examples. For the sake of variety, we use
9875 the predicate ``\verb|_==_|'' here. The following sequence proves
9876 associativity of ``\verb|_+_|''.
9877 \begin{vvtm}
9878 \begin{ccode}
9879 ** proving associativity of _+_
9880 open SIMPLE-NAT
9881 ops a b c : -> Nat .
9882 reduce 0 + (a + b) == (0 + a) + b .
9883 eq (a + b) + c = a + (b + c) . ** hypothesis
9884 reduce s(a) + (b + c) = (s(a) + b) + c .
9885 close
9886 \end{ccode}
9887 \end{vvtm}
9888 The session goes as follows.
9889 \begin{vvtm}
9890 \begin{ccode}
9891 CafeOBJ> open SIMPLE-NAT
9892
9893 -- opening module SIMPLE-NAT_.* done.
9894
9895 %SIMPLE-NAT> ops a b c : -> Nat .
9896
9897 %SIMPLE-NAT> reduce 0 + (a + b) == (0 + a) + b .
9898 _*
9899 -- reduce in % : 0 + (a + b) == (0 + a) + b
9900 true : Bool
9901 (0.000 sec for parse, 3 rewrites(0.000 sec), 11 matches)
9902 %SIMPLE-NAT> eq (a + b) + c = a + (b + c) .
9903
9904 %SIMPLE-NAT> reduce s(a) + (b + c) == (s(a) + b) + c .
9905 *
9906 -- reduce in % : s(a) + (b + c) == (s(a) + b) + c
9907 true : Bool
9908 (0.010 sec for parse, 5 rewrites(0.000 sec), 26 matches)
9909
9910 %SIMPLE-NAT> close
9911
9912 CafeOBJ>
9913 \end{ccode}
9914 \end{vvtm}
9915 Recall that when we gave a view for \verb|MONOID|
9916 (Section \ref{sec:p2-views-exs}), the associativity axiom
9917 remained to be proven. This obligation was discharged just now.
9918 Similarly, commutativity is proven by
9919 \begin{vvtm}
9920 \begin{ccode}
9921 ** proving commutativity of _+_
9922 open SIMPLE-NAT
9923 ops a b : -> Nat .
9924 reduce 0 + 0 == 0 + 0 .
9925 eq b + 0 = 0 + b . ** (inner) hypothesis
9926 reduce s(b) + 0 == 0 + s(b) .
9927 eq a + X:Nat = X + a . ** (outer) hypothesis -- *
9928 reduce 0 + s(a) == s(a) + 0 .
9929 eq b + s(a) = s(a) + b . ** (inner) hypothesis
9930 reduce s(b) + s(a) == s(a) + s(b) .
9931 close
9932 \end{ccode}
9933 \end{vvtm}
9934 The proof relies on double induction. And in this proof,
9935 the ``direction'' of equations is crucial: in giving the equation
9936 marked ``\verb|*|'', if you exchanged the sides, the subsequent evaluations
9937 would produce \verb|false|. To get \verb|true|, you need
9938 reverse applications (by using \verb|apply|).
9939
9940 \subsection{Nondeterministic Transitions}\label{sec:p2-proving-trans}
9941
9942 Since the current implementation of evaluation mechanism is deterministic,
9943 it is impossible to prove transition relations by
9944 \verb|execute| command alone. In such a case, the transition predicate
9945 ``\verb|_==>_|'' is often useful.
9946 As explained in Section \ref{sec:p2-transition-pred},
9947 ``\verb|_==>_|'' is supported by ``\verb|_=(*)=>_|'', which systematically
9948 checks transitions step by step.
9949
9950 Recall \verb|CHOICE-NAT| (Section \ref{exs:choice-nat}),
9951 and let us prove a simple fact that
9952 \begin{quotation}
9953 If $t$ transits to $u$, $t|t'$ also transits to
9954 $u$, for any $t'$.
9955 \end{quotation}
9956 \begin{vvtm}
9957 \begin{ccode}
9958 CafeOBJ> open CHOICE-NAT
9959
9960 -- opening module CHOICE-NAT.. done.
9961
9962 %CHOICE-NAT> ops a b c : -> Nat .
9963
9964 %CHOICE-NAT> trans a => c .
9965 _
9966 %CHOICE-NAT> reduce a | b ==> c .
9967
9968 -- reduce in % : a | b ==> c
9969 true : Bool
9970 (0.000 sec for parse, 6 rewrites(0.000 sec), 13 matches)
9971 %CHOICE-NAT> close
9972
9973 CafeOBJ>
9974 \end{ccode}
9975 \end{vvtm}
9976 For another simple example, suppose you want to prove another simple
9977 fact that
9978 \begin{quotation}
9979 If $t$ transits to $u$, $t + t'$ transits to $u + t'$, for any $t'$.
9980 \end{quotation}
9981 \begin{vvtm}
9982 \begin{ccode}
9983 CafeOBJ> open CHOICE-NAT
9984
9985 -- opening module CHOICE-NAT.. done.
9986
9987 %CHOICE-NAT> ops a b c : -> Nat .
9988
9989 %CHOICE-NAT> trans a => c .
9990 _
9991 %CHOICE-NAT> reduce a + b ==> c + b .
9992 *
9993 -- reduce in % : a + b ==> c + b
9994 true : Bool
9995 (0.000 sec for parse, 4 rewrites(0.000 sec), 15 matches)
9996 %CHOICE-NAT> close
9997
9998 CafeOBJ>
9999 \end{ccode}
10000 \end{vvtm}
10001 Note that, when using ``\verb|_==>_|'',
10002 evaluation should be by \verb|reduce| command, {\em not} by
10003 \verb|execute| cpmmand. Although ``\verb|_==>_|''
10004 represents transition relations, the reasoning is within pure
10005 equational logic.
10006
10007 There are a couple of switches that control the reduction procedure
10008 for ``\verb|_==>_|''. One is to get a trace. For illustration, you may
10009 rerun the first above session.\index{\texttt{exec}}\index{\texttt{trace}}
10010 \begin{vvtm}
10011 \begin{ccode}
10012 ... declaring a, b, c, and assert a => c as before
10013
10014 %CHOICE-NAT> set exec trace on
10015
10016 %CHOICE-NAT> reduce a | b ==> c .
10017
10018 -- reduce in % : a | b ==> c
10019 ** transition step 1-1 *******
10020 (): (a | b)
10021 =(1)=> a
10022 (): (a | b)
10023 =(1)=> b
10024 (1): (a | b)
10025 =(1)=> (c | b)
10026
10027 ** transition step 2-1 *******
10028 (): a
10029 =(2)=> c
10030 ** term : c
10031 matched to the pattern : c
10032 with the substitution : {}
10033 true : Bool
10034 (0.000 sec for parse, 6 rewrites(0.000 sec), 13 matches)
10035 %CHOICE-NAT>
10036 \end{ccode}
10037 \end{vvtm}
10038 There are three transitions from ``\verb+a | b+'', that is
10039 \begin{vvtm}
10040 \begin{ccode}
10041 trans N | N' => N .
10042 trans N | N' => N' .
10043 trans a => c .
10044 \end{ccode}
10045 \end{vvtm}
10046 respectively yielding \verb|a|, \verb|b|, and ``\verb+c | b+''.
10047 The first branch further transits to \verb|c|, which matches
10048 the given destination.
10049
10050 %!!!!!!!!!!!!!!!!!
10051 % What is exec condition?
10052 %A second switch controls conditional transitions. For example,
10053 %\begin{vvtm}
10054 %\begin{ccode}
10055 % %CHOICE-NAT>
10056 %\end{ccode}
10057 %\end{vvtm}
10058
10059 \subsection{Systematic Search for Transition Relations}
10060 \label{sec:p2-proving-trans-2}
10061
10062 ``\verb|_=(*)=>_|'' can often be used directly.
10063 Continuing the example of \verb|CHOICE-NAT|,
10064 \begin{vvtm}
10065 \begin{ccode}
10066 CafeOBJ> select CHOICE-NAT
10067
10068 CHOICE-NAT> reduce (0 | s(0)) | (s(s(0)) | (s(s(s(0))) | s(s(s(s(0))))))
10069 =(*)=> s(s(s(0))).
10070 -- reduce in CHOICE-NAT : (0 | s(0)) | (s(s(0)) | (s(s(s(0))) | s(
10071 s(s(s(0)))))) = ( * ) => s(s(s(0)))
10072 true : Bool
10073 (0.010 sec for parse, 51 rewrites(0.020 sec), 51 matches)
10074 CHOICE-NAT>
10075 \end{ccode}
10076 \end{vvtm}
10077 This was a very easy case, since all the arguments to ``\verb+_|_+'' are
10078 are irreducible and only pure transition steps are involved.
10079 A more demanding case is
10080 \begin{vvtm}
10081 \begin{ccode}
10082 CHOICE-NAT> reduce (0 | s(0)) + ((s(s(0)) | (s(s(s(0))) + 0))
10083 + s(s(s(s(0))))) =(*)=> s(s(0)) + (s(s(s(0))) + s(0)).
10084
10085 -- reduce in CHOICE-NAT : (0 | s(0)) + ((s(s(0)) | (s(s(s(0))) +
10086 0)) + s(s(s(s(0))))) = ( * ) => s(s(0)) + (s(s(s(0))) + s(0))
10087
10088 true : Bool
10089 (0.000 sec for parse, 35 rewrites(0.000 sec), 78 matches)
10090 CHOICE-NAT>
10091 \end{ccode}
10092 \end{vvtm}
10093 where the the system need to compute ``\verb|_+_|'' along the way.
10094
10095 If the search space can be large, ``\verb|=(*)=>|'' is a dangerous weapon.
10096 In such cases, you may limit the search up to a realistic step, with
10097 the set of predicates ``\verb|=(n)=>|'' where \verb|n| is a natural
10098 number (in the usual decimal notation) and indicates how many transit
10099 steps to take. For example,
10100 \begin{vvtm}
10101 \begin{ccode}
10102 CHOICE-NAT> reduce (0 | s(0)) | (s(s(0)) | ((s(s(s(0))) + 0) |
10103 s(s(s(s(0)))))) =(1)=> s(s(s(0))).
10104 -- reduce in CHOICE-NAT : (0 | s(0)) | (s(s(0)) | ((s(s(s(0))) +
10105 0) | s(s(s(s(0)))))) = ( 1 ) => s(s(s(0)))
10106 [Warning]: =(N)=> terminates, reaching preset maximum count of transitions.
10107 false : Bool
10108 (0.010 sec for parse, 13 rewrites(0.000 sec), 16 matches)
10109 CHOICE-NAT>
10110 \end{ccode}
10111 \end{vvtm}
10112 which is a correct result, since the first argument cannot transit to
10113 the second in one step.
10114
10115 Another way to limit the search is by a switch.\index{\texttt{exec}}
10116 \index{\texttt{limit}}
10117 \begin{vvtm}
10118 \begin{ccode}
10119 CafeOBJ> set exec limit 40
10120 \end{ccode}
10121 \end{vvtm}
10122 limits the maximal transition step to 40. The default limit is quite
10123 large.
10124
10125 As the above sessions show, ``\verb|_=(*)=>_|'' and
10126 ``\verb|_==>_|'' take into account equations as well as
10127 transitions. In most cases and with appropriate evaluation strategies,
10128 ``\verb|_==>_|'' does give correct answers on transition relations.
10129 However, it is in general impossible always to obtain correct answers
10130 on transition relations between {\em equivalence classes} of terms.
10131 For example, consider a module
10132 \begin{vvtm}
10133 \begin{ccode}
10134 module M {
10135 [ S ]
10136 ops a b c d : -> S
10137 eq a = b .
10138 trans a => c .
10139 eq c = d .
10140 }
10141 \end{ccode}
10142 \end{vvtm}
10143 and a theorem
10144 \begin{vvtm}
10145 \begin{ccode}
10146 trans a => d .
10147 \end{ccode}
10148 \end{vvtm}
10149 The system does not evaluate the corresponding term to \verb|true|, as
10150 \begin{vvtm}
10151 \begin{ccode}
10152 M> reduce a ==> d .
10153
10154 -- reduce in M : a ==> d
10155 > pre normalization : a
10156 == b
10157 -- no more possible transition for : a
10158 b ==> d : Bool
10159 (0.000 sec for parse, 4 rewrites(0.000 sec), 12 matches)
10160 M>
10161 \end{ccode}
10162 \end{vvtm}
10163 This is because the system reduces \verb|a| to an irreducible form
10164 (\verb|b|) before trying a systematic search. If it had tried
10165 a search before normalising, it would have proved the theorem.
10166 There is a switch that tells the system to forget about normalisation.
10167 \begin{vvtm}
10168 \begin{ccode}
10169 M> set exec normalize on
10170
10171 M>> reduce a ==> d .
10172
10173 -- reduce in M : a ==> d
10174
10175 ** transition step 1-1 *******
10176 (): a
10177 =(1)=> c
10178 < post nomalization : c
10179 == d
10180 ** term : d
10181 matched to the pattern : d
10182 with the substitution : {}
10183 true : Bool
10184 (0.000 sec for parse, 4 rewrites(0.010 sec), 6 matches)
10185
10186 M>
10187 \end{ccode}
10188 \end{vvtm}
10189 The result was satisfactory. This switch is hopeless if interleaving
10190 applications of equations and transitions are necessary. In
10191 \begin{vvtm}
10192 \begin{ccode}
10193 module M' {
10194 [ S ]
10195 ops a b c d e : -> S
10196 eq [e1] a = b .
10197 eq [e2] b = c .
10198 trans [t] b => d .
10199 eq [e3] d = e .
10200 }
10201 \end{ccode}
10202 \end{vvtm}
10203 to prove
10204 \begin{vvtm}
10205 \begin{ccode}
10206 trans a => e .
10207 \end{ccode}
10208 \end{vvtm}
10209 you have to use \verb|e1|, and only that.
10210
10211 \subsection{Behavioural Equivalence}\label{sec:p2-proving-beh-eq}
10212
10213 Recall \verb|COUNTER| (Section \ref{exs:counter}) and let us prove
10214 a behavioural equation
10215 \begin{vvtm}
10216 \begin{ccode}
10217 beq add(N:Nat, add(N':Nat, C:Counter)) = add(N', add(N, C)) .
10218 \end{ccode}
10219 \end{vvtm}
10220 As explained, this equation holds iff two terms act
10221 identically on every observation. This theorem may be proved
10222 by induction on the length of observations
10223 (called {\em context induction}\index{context indution}).
10224 Since \verb|add|, \verb|read| are the only ``method'' and ``attribute''
10225 respectively, every observation is an application of \verb|read|
10226 after a finite number of applications of \verb|add|.
10227
10228 A necessary command sequence is as follows. Since the proof
10229 uses associativity and commutativity of ``\verb|_+_|'' on \verb|Nat|,
10230 which were proven
10231 in Section \ref{sec:p2-proof-by-induction}, they are stated as equations.
10232 \begin{vvtm}
10233 \begin{ccode}
10234 open COUNTER
10235 ops m n p : -> Nat .
10236 op c : -> Counter .
10237 op addn : Counter -> Counter .
10238 eq m + n = n + m .
10239 eq N:Nat + (M:Nat + P:Nat) = (N + M) + P .
10240 reduce read(add(m, add(n, c))) == read(add(n, add(m, c))) .
10241 eq read(addn(add(m, add(n, c)))) = read(addn(add(n, add(m, c)))) .
10242 reduce read(add(p, addn(add(m, add(n, c))))) ==
10243 read(add(p, addn(add(n, add(m, c))))) .
10244 close
10245 \end{ccode}
10246 \end{vvtm}
10247 Alternatively, you may use ``\verb|_=*=_|''
10248 (Section \ref{sec:p2-beh-equiv-pred})
10249 and get a proof by {\em coinduction}\index{coinduction}.
10250 \begin{vvtm}
10251 \begin{ccode}
10252 CafeOBJ> open COUNTER
10253
10254 -- opening module COUNTER_.*done.
10255
10256 %COUNTER> ops m n : -> Nat .
10257
10258 %COUNTER> op c : -> Counter .
10259
10260 %COUNTER> eq m + n = n + m .
10261 _
10262
10263 %COUNTER> eq N:Nat + (M:Nat + P:Nat) = (N + M) + P .
10264
10265 %COUNTER> reduce add(m, add(n, c)) =*= add(n, add(m, c)) .
10266 *
10267 -- reduce in % : add(m,add(n,c)) =*= add(n,add(m,c))
10268 true : Bool
10269 (0.000 sec for parse, 9 rewrites(0.000 sec), 37 matches)
10270 %COUNTER> close
10271
10272 CafeOBJ>
10273 \end{ccode}
10274 \end{vvtm}
10275 The reduction went as desired, since the system automatically
10276 added an equation
10277 \begin{vvtm}
10278 \begin{ccode}
10279 eq hs1:Counter =*= hs2:Counter = read(hs1:Counter) == read(hs2:Counter) .
10280 \end{ccode}
10281 \end{vvtm}
10282 once ``\verb|_=*=_|'' was shown to be a congruence
10283 (Section \ref{sec:p2-hsort-beget-axs}).
10284
10285 As an aside, you may wonder why two axioms are given in these forms:
10286 commutativity is stated with constants, while associativity is with
10287 variables. The reason is that a straightforward statement of commutativity
10288 \begin{vvtm}
10289 \begin{ccode}
10290 eq M:Nat + N:Nat = N + M .
10291 \end{ccode}
10292 \end{vvtm}
10293 engenders infinite rewrite sequences. The trick here is to give an
10294 axiom that restricts the matching power of the system so that,
10295 only in the special instances when you need for this special proof,
10296 the axiom is usable as a rewrite rule.
10297
10298 \subsection{Behavioural Transition}\label{sec:p2-proving-beh-trans}
10299
10300 To illustrate proofs on behavioural transitions, we define
10301 counters of wobbly numbers.\label{exs:w-counter}
10302 \begin{vvtm}
10303 \begin{ccode}
10304 module W-COUNTER {
10305 protecting (CHOICE-NAT)
10306 *[ Counter ]*
10307 bop read : Counter -> Nat
10308 bop add : Nat Counter -> Counter
10309 eq read(add(N:Nat, C:Counter)) = N + read(C) .
10310 }
10311 \end{ccode}
10312 \end{vvtm}
10313 The structure is the same as \verb|COUNTER|, and the difference is
10314 in the imported \verb|CHOICE-NAT|. We would like to
10315 prove a behavioural transition relation
10316 \begin{vvtm}
10317 \begin{ccode}
10318 btrans add(M:Nat | N:Nat, C:Counter) => add(N, C) .
10319 \end{ccode}
10320 \end{vvtm}
10321 An element of hidden sort behaviourally transits to another
10322 iff every observation of the former transits to the same
10323 observation of the latter. This definition leads to
10324 a proof by context induction, as in the previous section. The session goes
10325 as follows.
10326 \begin{vvtm}
10327 \begin{ccode}
10328 CafeOBJ> open W-COUNTER
10329
10330 -- opening module W-COUNTER_.*done.
10331
10332 %W-COUNTER> ops m n p : -> Nat .
10333
10334 %W-COUNTER> op c : -> Counter .
10335
10336 %W-COUNTER> op addn : Counter -> Counter .
10337
10338 %W-COUNTER> reduce read(add(m | n, c)) ==> read(add(m, c)) .
10339 _*
10340 -- reduce in % : read(add(m | n,c)) ==> read(add(m,c))
10341 true : Bool
10342 (0.010 sec for parse, 6 rewrites(0.010 sec), 32 matches)
10343 %W-COUNTER> trans read(addn(add(m | n, c))) => read(addn(add(m, c))) .
10344
10345 %W-COUNTER> reduce read(add(p, addn(add(m | n, c))))
10346 ==> read(add(p, addn(add(m, c)))) .
10347 *
10348 -- reduce in % : read(add(p,addn(add(m | n,c)))) ==> read(add(p,addn(
10349 add(m,c))))
10350 true : Bool
10351 (0.000 sec for parse, 4 rewrites(0.000 sec), 11 matches)
10352 %W-COUNTER> close
10353
10354 CafeOBJ>
10355 \end{ccode}
10356 \end{vvtm}
10357 The command sequence is quite similar to the one in the previous section.
10358 Or, relying on ``\verb|=*=|'', you may be rest assured by just one
10359 reduction
10360 \begin{vvtm}
10361 \begin{ccode}
10362 %W-COUNTER> reduce read(add(m | n, c)) ==> read(add(m, c)) .
10363 \end{ccode}
10364 \end{vvtm}
10365 which, by itself, is a proof by coinduction. A difference from the case of
10366 behavioural equivalence is that the system does not suppy an
10367 axiom like
10368 \begin{vvtm}
10369 \begin{ccode}
10370 eq hs1:Counter =*= hs2:Counter = read(hs1:Counter) == read(hs2:Counter) .
10371 \end{ccode}
10372 \end{vvtm}
10373 so you have to be satisfied by handfeed reduction commands for each
10374 ``attribute''.
10375
10376 %\subsection{Congruences for Coinduction}\label{sec:p2-congruence-def}
10377 %
10378 %Since the predicate ``\verb|=*=|'' is not omnipotent, you may want to
10379 %define your own congruences on hidden sorts. Consider a parametric module
10380 %\verb|STREAM|.
10381 %\begin{vvtm}
10382 %\begin{ccode}
10383 % module* STREAM (X :: ONE) {
10384 % *[ Stream ]*
10385 % bop head : Stream -> Elt
10386 % bop tail : Stream -> Stream
10387 % op merge : Stream Stream -> Stream
10388 % vars S S' : Stream
10389 % eq head(merge(S,S')) = head(S) .
10390 % beq tail(merge(S,S')) = merge(S',tail(S)) .
10391 % }
10392 %\end{ccode}
10393 %\end{vvtm}
10394
10395 \chapter{Built-in Modules}\label{sec:p2-builtin-module}
10396
10397 You may define any data types in \cafeobj, so in principle the need for
10398 built-in data types does not arise. So is the principle, but
10399 considerations about
10400 convenience and tolerable performance dictate otherwise. Some
10401 frequently used data types are defined in {\em built-in module}s.
10402 \index{built-in module} In addition,
10403 the system relies on a couple of {\em kernel module}s,\index{kernel module}
10404 which are located near its jugular.
10405 A few commands, such \verb|reset| (Section \ref{sec:p2-startup}) are
10406 closely related to these modules.
10407
10408 Listed below are the kernels and built-ins.
10409 (As explained in Section \ref{sec:p2-init-opt}, you may supply your
10410 own built-ins, or even override the standard built-ins.)
10411 \begin{itemize}
10412 \item[] \textbf{ Systems}. Some modules are vital to the behaviour of
10413 the system. Unless you are ready to help us tune the system, they
10414 need not concern you.
10415 \item[] \textbf{ Booleans}. The module \verb|BOOL| and the sort \verb|Bool|
10416 have appeared frequently. Actually, \verb|Bool| is declared in a
10417 submodule imported by \verb|BOOL|. And \verb|RWL| is a module that
10418 declares transition predicate ``\verb|_==>_|''.
10419 \item[] \textbf{ Numbers}. The system supports usual arithmetics within
10420 a usual number system. The modules involved are
10421 \verb|NAT|, \verb|INT|, \verb|RAT| and \verb|FLOAT|.
10422 \item[] \textbf{ Strings}. \verb|QID| (quoted identifier),
10423 \verb|CHARACTER|, \verb|STRING| define operations on characters
10424 and strings.
10425 \item[] \textbf{ Pairs}. A simple pair, triple, and quadruple are defined
10426 in \verb|2TUPLE|, \verb|3TUPLE|, and \verb|4TUPLE|.
10427 \item[] \textbf{ Miscellaneous}. For historical reasons, \verb|PROPC|
10428 (Section \ref{exs:propc}), \verb|TRIV| (Section \ref{exs:triv})
10429 are built-in modules.
10430 \end{itemize}
10431 There are also a set of library modules and examples in the
10432 distribution package.
10433
10434 \begin{warning}
10435 Elements in built-in sorts are defined as corresponding Lisp objects,
10436 and the system may compute them by stealth. For example,
10437 an expression \verb|2.1e1| is an element of \verb|Float|, and
10438 is {\em identified} with \verb|21.0|; \verb|2/1| is an element of
10439 \verb|Rat|, and is identified with \verb|2|.
10440 \end{warning}
10441
10442 \begin{warning}
10443 The built-ins and their definitions may change.
10444 \end{warning}
10445
10446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10447 \appendix
10448 \chapter{Summary of \cafeobj~~syntax}
10449
10450 Here is a brief summary of lexical conventions and syntactic definitions
10451 used in \cafeobj~codes and commands.
10452
10453 \section{Lexical Analysis} \label{sec:lex}
10454
10455 On the lexical surface a \cafeobj~code is a sequence of tokens and
10456 separators.
10457 A {\em token} is a sequence of printable ASCII characters (octal
10458 40 through 176)\footnote{The current system accepts Unicode
10459 characters also, but this is beyond the definition of
10460 the language.}, and is classified as either a self-terminating
10461 character, an identifier, or an operator symbol. Each class
10462 is explained below.
10463
10464 A {\em separator} is a blank character --- space, vertical tab,
10465 horizontal tab, carriage return, newline, or from feed.
10466
10467 On the one hand, any mumber of separators may appear between tokens.
10468 On the other hand, one or more separators must appear between any two
10469 adjacent non-self-terminating tokens\footnote{The same rule
10470 applies to terms.
10471 Further, if an operator symbol contains blanks or
10472 self-terminating characters, it is sometimes neccessary to enclose a
10473 term with such operator as top by parentheses for disambiguation.}.
10474
10475 \subsection{Reserved Words} \label{sec:keywords}
10476
10477 There is {\em no} reserved word.
10478 One can use such keywords such as \sym{module}, \sym{op}, \sym{var},
10479 or \sym{signature}, etc. for identifiers or operator symbols.
10480
10481 \subsection{Self-Terminating Characters} \label{sec:lex-selfterminating}
10482
10483 A self-terminating character is a printable ASCII character
10484 which by itself constitutes a token. There are seven such characters.
10485 \begin{center}
10486 \begin{minipage}{0.5\textwidth}
10487 \begin{snugshade}
10488 \begin{tabular}{lllllll}
10489 \verb|(| & \verb|)| & \verb|,| & \verb|[| & \verb|]| & \verb|{| & \verb|}|
10490 \end{tabular}
10491 \end{snugshade}
10492 \end{minipage}
10493 \end{center}
10494 %%
10495 \subsection{Identifiers} \label{sec:lex-identifier}
10496
10497 An \textit{ident} (identifier)
10498 is a sequence of any printable ASCII characters other than
10499 \begin{center}
10500 \begin{minipage}{0.5\textwidth}
10501 \begin{snugshade}
10502 \begin{tabular}{l}
10503 self-terminating characters \\
10504 \verb|.| (period) \\
10505 \verb|"| (double quote) \\
10506 \end{tabular}
10507 \end{snugshade}
10508 \end{minipage}
10509 \end{center}
10510
10511 The \cafeobj~reader is case-sensitive, and \verb|Nat| and \verb|nat| are
10512 different identifiers.
10513 \textit{ident}s appear as module names, view names,
10514 parameter names, sort names, variables names,
10515 slot names, and labels.
10516
10517 \subsection{Operator Symbols} \label{sec:opsymbol}
10518
10519 An \nonterm{operator\_symbol} is a sequence of any ASCII characters,
10520 including non-printables, except EOT (control-D).
10521
10522 An underbar has a special meaning in an operator symbol.
10523 It reserves the place where an argument is inserted.
10524 A single underbar \verb|_| cannot be an operator symbol.
10525
10526 \subsection{Comments} \label{sec:lex-comments}
10527
10528 A \nonterm{comment} is a sequence of characters which begins with one
10529 of the following four character sequences
10530 \begin{center}
10531 \begin{minipage}{0.5\textwidth}
10532 \begin{snugshade}
10533 \begin{tabular}{ll}
10534 "\verb|--|" &
10535 "\verb|-->|" \\
10536 "\verb|**|" &
10537 "\verb|**>|"
10538 \end{tabular}
10539 \end{snugshade}
10540 \end{minipage}
10541 \end{center}
10542 and that ends with a newline character. In between, a comment may
10543 contain only printable ASCII characters and horizontal tabs.
10544
10545 Comments may act as separators, but their appearance is restricted
10546 (See the next section).
10547
10548 \newpage
10549 \section{\cafeobj~ Syntax} \label{sec:cafeobj-syntax}
10550 We use an extended BNF grammar to define the syntax. The general form
10551 of a production is
10552 \begin{vvtm}
10553 \begin{syntax}
10554 \synindent\synindent nonterminal &::=& alternative \alt alternative \alt \cdots \alt alternative
10555 \end{syntax}
10556 \end{vvtm}
10557 The following extensions are used:
10558 \begin{center}
10559 \begin{fminipage}{0.7\textwidth}
10560 \begin{tabular}{ll}
10561 $\alpha$ $\cdots$ & a list of one or more $\alpha$s. \\
10562 $\alpha$, $\cdots$ & a list of one or more $\alpha$s separated by commas: \\
10563 & ``$\alpha$'' or ``$\alpha$, $\alpha$'' or ``$\alpha$, $\alpha$, $\alpha$'', etc. \\
10564 $\seqof{\alpha}$ & \textbf{\{} and \textbf{\}} are meta-syntactical
10565 brackets \\
10566 & treating $\alpha$ as one syntactic category. \\
10567 $\optn{\alpha}$ & an optional $\alpha$: `` '' or ``$\alpha$''.
10568 \end{tabular}
10569 \end{fminipage}
10570 \end{center}
10571 Nonterminal symbols appear in \textit{italic face}. Terminal symbols
10572 appear in the face like this: ``\sym{terminal}'', and may be
10573 surrounded by `` and '' for emphasis or to avoid confusion
10574 with meta characters used in the extended BNF. We will refer terminal
10575 symbols other than self-terminating characters (see section
10576 ~\ref{sec:lex-selfterminating}) as \textit{keyword}s in this document.
10577
10578 \subsection{\cafeobj~Codes}
10579 A \cafeobj~code is a sequence of modules, views, and evaluation commands.
10580 \begin{vvtm}
10581 \begin{syntax}
10582 \synindent\synindent spec &::=& \seqof{module \alt view \alt eval} \cdots
10583 \end{syntax}
10584 \end{vvtm}
10585 %%%
10586 \newpage
10587 \subsection{Modules} \label{sec:syntax-module}
10588 where a \nonterm{module} is
10589 \begin{vvtm}
10590 \begin{syntax}
10591 module &::=& module\_type\; module\_name \;
10592 \optn{parameters} \;
10593 \optn{principal\_sort} \\
10594 && \sym{``\{''}\; module\_elt\cdots \; \sym{``\}''} \\
10595 \synindent module\_type &::=& \sym{module} \alt \sym{module!} \alt
10596 \sym{module*}\\
10597 \synindent module\_name &::=& ident
10598 % &\footnote{The nonterminal \textit{ident} is for identifiers and
10599 % will be defined in the section ~\ref{sec:identifier}.}
10600 \\
10601 parameters &::=& \sym{``(''}\; parameter, \cdots \sym{``)''}\\
10602 \synindent parameter &::=&
10603 \optn{\sym{protecting}\alt\sym{extending}\alt\sym{including}}\;
10604 paramter\_name \;
10605 \sym{::}\; module\_expr
10606 %& \footnote{\textit{module\_expr} is defined in
10607 % the section ~\ref{sec:modexpr}.}\footnote{If optional
10608 % $\optn{\sym{protecting}\alt\sym{extending}\alt\sym{including}}$ is omitted, it is
10609 % defaulted to \sym{protecting}.}
10610 \\
10611 \synindent parameter\_name &::=& ident \\
10612 principal\_sort &::=& \sym{principal-sort}\; sort\_name \\
10613 % module\_elt &::=& import \alt sort \alt record \alt operator \alt
10614 module\_elt &::=& import \alt sort \alt operator \alt
10615 % module\_elt &::=& import \alt sort \alt operator \alt
10616 variable \alt axiom \alt comment
10617 % &\footnote{\nonterm{comment} is
10618 % descussed in section ~\ref{sec:comments}.}
10619 \\
10620 import &::=& \seqof{\sym{protecting}\alt\sym{extending}\alt\sym{including}\alt\sym{using}}
10621 \sym{``(''}\;module\_expr\;\sym{``)''}\\
10622 sort & ::= & visible\_sort \alt hidden\_sort \\
10623 \synindent visible\_sort & ::=& \sym{``[''}\; sort\_decl, \cdots \; \sym{``]''} \\
10624 \synindent hidden\_sort & ::=& \sym{``*[''}\; sort\_decl, \cdots \; \sym{``]*''} \\
10625 \synindent sort\_decl &::=& sort\_name\; \cdots\; \optn{supersorts\; \cdots} \\
10626 \synindent supersorts &::=& <\; sort\_name\; \cdots \\
10627 \synindent sort\_name &::=& sort\_symbol\optn{qualifier}
10628 % & \footnote{There must not be any separators (see section
10629 % ~\ref{sec:lex}) between \nonterm{ident} and \nonterm{qualifier}.}
10630 \\
10631 \synindent sort\_symbol &::=& ident \\
10632 \synindent qualifier &::=& \sym{``.''}module\_expr\optn{qualifier}\\
10633 %% record &::=& \sym{record}\; sort\_name\; \optn{super\;\cdots}\;
10634 %% \sym{``\{''}\; \seqof{slot \alt comment}\cdots \; \sym{``\}''} \\
10635 %% \synindent super &::=&
10636 %% \sym{``[''}\;sort\_name\;\optn{\sym{``(''}\; slot\_rename,\cdots
10637 %% \sym{``)''}}\;\sym{``]''} \\
10638 %% \synindent slot &::=& slot\_name : sort\_name \alt
10639 %% slot\_name\;\sym{=}\;\sym{``(''}term\sym{``)''}\;:\; sort\_name \\
10640 %% \synindent slot\_name &::=& ident \\
10641 %% \synindent slot\_rename &::=& slot\_name\; \verb|->|\; slot\_name\\
10642 operator &::=& \seqof{\sym{op}\alt\sym{bop}}
10643 \; operator\_symbol\; : \; \optn{arity}\; \verb|->|\;
10644 coarity \; \optn{op\_attrs}
10645 % &\footnote{\nonterm{operator\_symbol} is defined in section
10646 % ~\ref{sec:opsymbol}.}
10647 \\
10648 \synindent arity &::=& sort\_name \cdots \\
10649 \synindent coarity &::=& sort\_name \\
10650 \synindent op\_attrs &::=& \sym{``\{''}\;op\_attr\cdots\;\sym{``\}''} \\
10651 \synindent op\_attr &::=& \sym{constr} \alt
10652 \sym{associative} \alt \sym{commutative}
10653 \alt\sym{idempotent}
10654 \alt \seqof{\sym{id:} \alt \sym{idr:}} \sym{``(''} \; term\; \sym{``)''}
10655 %&\footnote{The general syntax of \nonterm{term} is defined in section
10656 % ~\ref{sec:term}.}
10657 \\
10658 &\alt & \sym{strat: ``(''}\; natural \cdots\; \sym''{)''}
10659 \alt\sym{prec:}\; natural
10660 \alt \sym{l-assoc} \alt \sym{r-assoc} \alt \sym{coherent}
10661 % &\footnote{\nonterm{natural} is a natural number written in ordinal
10662 % arabic notation.}
10663 \\
10664 variable &::=& \sym{var}\; var\_name\; :\; sort\_name \alt
10665 \sym{vars} \; var\_name\cdots\; :\; sort\_name \\
10666 \synindent var\_name &::=& ident \\
10667 axiom &::=& equation \alt cequation\alt transition \alt ctransition \\
10668 \synindent equation &::=& \seqof{\sym{eq} \alt \sym{beq}}\;
10669 \optn{label}\; term \; \sym{=} \; term\; \sym{``.''}\\
10670 \synindent cequation &::=& \seqof{\sym{ceq} \alt \sym{bceq}}\;
10671 \optn{label}\; term \; \sym{=}
10672 \; term\; \sym{if}\; term\;\sym{``.''} \\
10673 \synindent transition &::=& \seqof{\sym{trans} \alt \sym{btrans}}\;
10674 \optn{label}\; term \; \verb|=>| \; term \;\sym{``.''}\\
10675 \synindent ctransition &::=& \seqof{\sym{ctrans} \alt \sym{bctrans}}\;
10676 \optn{label}\; term \; \verb|=>| \; term\; \sym{if}\; term\;\sym{``.''}\\
10677 \synindent label &::=& \sym{``[''}\; ident\; \sym{``]:''}
10678 \end{syntax}
10679 \end{vvtm}
10680 %%
10681 \newpage
10682 \subsection{Module Expression}
10683 \label{sec:modexpr}
10684 A \nonterm{module\_expr} is
10685 \begin{vvtm}
10686 \begin{syntax}
10687 module\_expr &::=& module\_name \alt sum \alt rename \alt instantiation
10688 \alt \sym{``(''} module\_expr \sym{``)''} \\
10689 sum &::=& module\_expr\; \seqof{+\;\; module\_expr} \cdots \\
10690 rename &::=& module\_expr\;\sym{*}\;\sym{``\{''} rename\_map,\cdots
10691 \sym{``\}''} \\
10692 % instantiation &::=& module\_expr\;\sym{``(''}
10693 % \seqof{ident \optn{qualifier}\;\verb|<=|\;aview},\cdots
10694 % \sym{``)''}
10695 instantiation &::=& module\_expr\;\sym{``(''}\textbf{\{}\;ident
10696 [ qualifier ]\; \verb|<=|\; aview\textbf{\}},\;\cdots\;\sym{``)''}
10697 \\
10698 rename\_map &::=& sort\_map \alt op\_map \\
10699 sort\_map &::=& \seqof{\sym{sort} \alt \sym{hsort}}\;
10700 sort\_name \; \verb|->|\; ident \\
10701 op\_map &::=& \seqof{\sym{op} \alt \sym{bop}}\;
10702 op\_name\;\verb|->|\;operaotr\_symbol \\
10703 op\_name &::=& operator\_symbol\alt
10704 \sym{``(''}operator\_symbol\sym{``)''}qualifier\\
10705 aview &::=& view\_name\alt module\_expr\\
10706 & \alt &
10707 \sym{view to}\; module\_expr\;\sym{``\{''} view\_elt,\cdots \sym{``\}''} \\
10708 view\_name &::=& ident \\
10709 view\_elt &::=& sort\_map \alt op\_view \alt variable \\
10710 op\_view &::=& op\_map \alt term\; \verb|->|\; term
10711 \end{syntax}
10712 \end{vvtm}
10713 When a module expression is not fully parenthesized, the proper
10714 nesting of subexpressions may be ambiguous. The following precedence rule
10715 is used to resolve such ambiguity:
10716 \[ \nonterm{sum} < \nonterm{rename} < \nonterm{instantiation} \]
10717
10718 \subsection{Views} \label{sec:syntax-view}
10719 and a \nonterm{view} is
10720 \begin{vvtm}
10721 \begin{syntax}
10722 view &::=& \sym{view}\; view\_name\; \sym{from}\; module\_expr\; \sym{to}
10723 \; module\_expr \\
10724 && \sym{``\{''}\; view\_elt, \cdots \; \sym{``\}''} \\
10725 \end{syntax}
10726 \end{vvtm}
10727
10728 \subsection{Evaluation Commands} \label{sec:syntax-eval}
10729
10730 and an \nonterm{eval} is
10731 \begin{vvtm}
10732 \begin{syntax}
10733 eval & ::= & \seqof{\sym{reduce}\alt\sym{behavioural-reduce}
10734 \alt\sym{execute}}\; context\;
10735 term\; \sym{``.''} \\
10736 context & ::=& \sym{in}\; module\_expr\;\sym{:}
10737 \end{syntax}
10738 \end{vvtm}
10739 The interpreter has a notion of \textit{current module} which
10740 is specified by a \nonterm{module\_expr} and establishes a context.
10741 If it is set, \nonterm{context} can be omitted.
10742
10743 \subsection{Terms} \label{sec:syntax-term}
10744
10745 A \nonterm{term} is a sequence of any ASCII characters except EOT.
10746
10747 \subsection{Sugars and Abbreviations} \label{sec:syntax-abbrev}
10748
10749 \paragraph{Module type}
10750 There are following abbreviations for \nonterm{module\_type}.
10751 \begin{center}
10752 \begin{minipage}{0.3\textwidth}
10753 % \begin{vvtm}
10754 \begin{tabular}{ll}\hline
10755 Keyword & Abbriviation \\\hline
10756 \sym{module} & \sym{mod}\\
10757 \sym{module!} & \sym{mod!} \\
10758 \sym{module*} & \sym{mod*}\\\hline
10759 \end{tabular}
10760 % \end{vvtm}
10761 \end{minipage}
10762 \end{center}
10763
10764 %%%
10765
10766 \paragraph{Module Declaration}
10767 \nonterm{make} is a short hand for declaring module of
10768 name \nonterm{module\_name} which imports \nonterm{module\_expr} with
10769 protecting mode.
10770 \begin{vvtm}
10771 \begin{syntax}
10772 make &::=& \sym{make}\;module\_name\;\sym{``(''}\; module\_expr\;\sym{``)''}
10773 \end{syntax}
10774 \end{vvtm}
10775
10776 \begin{ccode}
10777 make FOO (BAR * {sort Bar -> Foo})
10778 \end{ccode}
10779
10780 is equivalent to
10781
10782 \begin{ccode}
10783 module FOO { protecting (BAR * {sort Bar -> Foo}) }
10784 \end{ccode}
10785
10786
10787 \paragraph{Principal Sort}
10788 \sym{principal-sort} can be abbriviated to \sym{psort}.
10789
10790 \paragraph{Import Mode}
10791 For import modes, the following abbriviations can be used:
10792 \begin{center}
10793 \begin{minipage}{0.31\textwidth}
10794 % \begin{vvtm}
10795 \begin{tabular}{ll}\hline
10796 Keyword & Abbriviation \\\hline
10797 \sym{protecting} & \sym{pr} \\
10798 \sym{extending} & \sym{ex} \\
10799 \sym{including} & \sym{inc} \\
10800 \sym{using} & \sym{us} \\\hline
10801 \end{tabular}
10802 % \end{vvtm}
10803 \end{minipage}
10804 \end{center}
10805
10806 \paragraph{Simultaneous Operator Declaration}
10807 Several operators with the same arity, coarity and operator attributes
10808 can be declared at once by \sym{ops}.
10809 The form
10810 \begin{vvtm}
10811 \begin{syntax}
10812 \sym{ops}\; operator\_symbol_1 \cdots operator\_symbol_n\;
10813 :\; arity\; \verb|->|\; coarity\; op\_attrs
10814 \end{syntax}
10815 \end{vvtm}
10816 is just equivalent to the following multiple operator declarations:
10817 \begin{vvtm}
10818 \begin{syntax}
10819 \sym{op}\; & operator\_symbol_1\;:\;arity\;\verb|->|\;coarity\;op\_attrs
10820 \\
10821 &\vdots\\
10822 \sym{op}\; & operator\_symbol_n\;:\;arity\;\verb|->|\;coarity\;op\_attrs
10823 \end{syntax}
10824 \end{vvtm}
10825 \sym{bops} is the counterpart of \sym{ops} for behavioural operators.
10826 \begin{vvtm}
10827 \begin{syntax}
10828 \sym{bops}\; operator\_symbol\;\cdots\; : \; arity\;\verb|->|\;
10829 coarity \; op\_attrs
10830 \end{syntax}
10831 \end{vvtm}
10832 and the effect is the same as many \nonterm{operator\/}s as there are
10833 \nonterm{operator\_symbols\/}s.
10834
10835 In simultaneous declarations, parentheses are sometimes necessary
10836 to separate operator symbols. This is always required if an operator
10837 symbol contains dots, blank characters or underscores.
10838
10839 \paragraph{Predicate}
10840 A \nonterm{predicate} is a syntactic sugar for operator declarations with
10841 arity \verb|Bool|, and is defined as
10842 \begin{vvtm}
10843 \begin{syntax}
10844 predicate &::=&
10845 \sym{pred}\;operator\_symbol\;:\;arity\;\optn{op\_attrs}
10846 % &\footnote{You cannot use \nonterm{sort\_name} of the same
10847 % character sequence as that of any keywords, i.e., \sym{module},
10848 % \sym{op}, \sym{vars}, etc. in \nonterm{arity}.}
10849 \end{syntax}
10850 \end{vvtm}
10851 The form
10852 \begin{vvtm}
10853 \begin{syntax}
10854 \sym{pred}\;operator\_symbol\;:\;arity\;op\_attrs
10855 \end{syntax}
10856 \end{vvtm}
10857 is equivalent to:
10858 \begin{vvtm}
10859 \begin{syntax}
10860 \sym{op}\;operator\_symbol\;:\;arity\;\verb|->|\; \sym{Bool} \;op\_attrs
10861 \end{syntax}
10862 \end{vvtm}
10863
10864 \paragraph{Operator Attributes}
10865 The following abbriviations are available:
10866 \begin{center}
10867 \begin{tabular}{ll}\hline
10868 Keyword & Abbriviation \\\hline
10869 \sym{associative} & \sym{assoc} \\
10870 \sym{commutative} & \sym{comm} \\
10871 \sym{idempotent} & \sym{idem} \\\hline
10872 \end{tabular}
10873 \end{center}
10874
10875 \paragraph{Axioms}
10876 For the keywords introducing axioms, the following
10877 abbriviations can be used:
10878 \begin{center}
10879 \begin{tabular}{ll|ll}\hline
10880 Keyword & Abbriviation & Keyword & Abbriviation \\\hline
10881 \sym{ceq} & \sym{cq} &
10882 \sym{bceq} & \sym{bcq} \\
10883 \sym{trans} & \sym{trns} &
10884 \sym{ctrans} & \sym{ctrns} \\
10885 \sym{btrans} & \sym{btrns} &
10886 \sym{bctrans} & \sym{bctrns} \\\hline
10887 \end{tabular}
10888 \end{center}
10889
10890 \paragraph{Blocks of Declarations}
10891 Importationss, signature definitions and
10892 axioms can be clusterd in blocks
10893 \nonterm{imports}, \nonterm{signature}, and \nonterm{axioms} respectively.
10894 \begin{vvtm}
10895 \begin{syntax}
10896 imports &::=&
10897 \sym{imports}\;\sym{``\{''}\\
10898 &&\synindent\seqof{import \alt comment}\cdots\\
10899 &&\sym{``\}''}\\
10900 signature &::=& \sym{signature}\;\sym{``\{''}\\
10901 % &&\synindent\seqof{sort \alt record \alt operator \alt
10902 &&\synindent\seqof{sort \alt operator \alt
10903 comment}\cdots\\
10904 &&\sym{``\}''}\\
10905 axioms &::=& \sym{axioms}\;\sym{``\{''}\\
10906 &&\synindent\seqof{variable \alt axiom\alt comment}\cdots\\
10907 &&\sym{``\}''}
10908 \end{syntax}
10909 \end{vvtm}
10910 \paragraph{Views}
10911 It is possible to identify parameters by positions, not by names.
10912 For example, if a parametric module is declared as
10913 \begin{ccode}
10914 module! FOO (A1 :: TH1, A2 :: TH2) { ... }
10915 \end{ccode}
10916 the form
10917 \begin{ccode}
10918 FOO(V1, V2)
10919 \end{ccode}
10920 is equivalent to
10921 \begin{ccode}
10922 FOO(A1 <= V1, A2 <= V2)
10923 \end{ccode}
10924
10925 Moreover, \sym{view to} construct in arguments of module
10926 instantiations can always be omitted. So,
10927 \begin{example}
10928 \kbd{FOO(A1} \verb|<=| \sym{view to} \nonterm{module\_expr}\{...\}\kbd{)}
10929 \end{example}
10930 can be written as
10931 \begin{example}
10932 \kbd{FOO(A1} \verb|<=| \nonterm{module\_expr}\{...\}\kbd{)}
10933 \end{example}
10934
10935 \paragraph{Evaluation}
10936 For \nonterm{eval}, the following abbriviations are available:
10937 \begin{center}
10938 \begin{tabular}{ll}\hline
10939 Keyword & Abbriviation \\\hline
10940 \sym{reduce} & \sym{red} \\
10941 \sym{bereduce} & \sym{bred} \\
10942 \sym{execute} & \sym{exec} \\\hline
10943 \end{tabular}
10944 \end{center}
10945
10946 %%%%%%%%%%%%%%%%%%%%%%%%
10947 \clearpage
10948
10949 \chapter{Command Summary}\label{sec:ref-cafeobj-cmds}
10950
10951 Following is the top level commands that do not constitute \cafeobj~codes.
10952
10953 \textit{}
10954 \begin{tabular}{r c l}
10955 command &::=& show $|$ describe $|$ set $|$ input $|$ check $|$
10956 regularise $|$ save $|$ restore $|$ save\_system \\
10957 &$|$& protect $|$ unprotect $|$ clean\_memo $|$ reset $|$ full\_reset $|$
10958 parse $|$ lisp\_call $|$ select $|$ open \\
10959 &$|$& close $|$ start $|$ apply $|$ match $|$
10960 choose $|$ find $|$ provide $|$ require $|$ autoload \\
10961 &$|$& eof $|$ prompt $|$ quit $|$ cd $|$ ls $|$ pwd $|$ help $|$
10962 shell\_call $|$ dribble $|$ period $|$ compile \\
10963 \\
10964 show &::=& \verb|show| $\{$ help $|$ module\_contents $|$
10965 switches $\}$ \\
10966 describe &::=& \verb|describe| $\{$ help $\}$ $|$ module\_contents $\}$ \\
10967 set &::=& \verb|set| $\{$ set\_help $|$ set\_switch $|$ set\_other $\}$ \\
10968 input &::=& \verb|input| file\_name \\
10969 protect &::=& \verb|protect| module\_name \\
10970 unprotect &::=& \verb|unprotect| module\_name \\
10971 full\_reset &::=& \verb|full-reset| $|$ $\{$ \verb|full| \verb|reset| $\}$ \\
10972 open &::=& \verb|open| module\_expression \\
10973 close &::=& \verb|close| \\
10974 start &::=& \verb|start| term ``\verb|.|'' \\
10975 parse &::=& \verb|parse| $[$ context $]$ term ``\verb|.|'' \\
10976 cd &::=& \verb|cd| file\_name \\
10977 quit &::=& \verb|quit| \\
10978 period &::=& ``\verb|.|'' \\
10979 shell\_call &::=& ``\verb|!|'' shell\_command \\
10980 provide &::=& \verb|provide| feature \\
10981 require &::=& \verb|require| feature $[$ file\_name $]$ \\
10982 ls &::=& \verb|ls| file\_name \\
10983 compile &::=& \verb|tram| \verb|compile| $[$ compile\_option $]$
10984 module\_expression \\
10985 compile\_option &::=& \verb|-exec| \\
10986 eof &::=& \verb|eof| \\
10987 save &::=& \verb|save| file\_name \\
10988 restore &::=& \verb|restore| file\_name \\
10989 clean\_memo &::=& \verb|clean| \verb|memo| \\
10990 dribble &::=& \verb|dribble| file\_name \\
10991 select &::=& \verb|select| module\_expresion \\
10992 lisp\_call &::=& $\{$ \verb|lisp| $|$ \verb|lispq| $\}$ S\_expression \\
10993 help &::=& ``\verb|?|'' \\
10994 autoload &::=& \verb|autoload| proc\_name \\
10995 find &::=& \verb|find| $\{$ \verb|rule| $|$ \verb|+rule| $|$ \verb|-rule|
10996 $\}$ \\
10997 match &::=& \verb|match| term\_specifier \verb|to| match\_pattern
10998 ``\verb|.|'' \\
10999 term\_specifier &::=& \verb|top| $|$ \verb|term| $|$ \verb|it| $|$ subterm
11000 $|$ ``\verb|(|'' term ``\verb|)|'' \\
11001 match\_pattern &::=& $\{$ $[$ \verb|all| $]$
11002 $\{$ \verb|rules| $|$ \verb|+rules| $|$ \verb|-rules| $\}$ $\}$ \\
11003 &$|$& term \\
11004 apply &::=& \verb|apply| action $[$ substitution $]$ range selection \\
11005 select &::=& \\
11006 check &::=& \\
11007 regularise &::=& \verb|regularize| module\_name \\
11008 \end{tabular}
11009 \textrm{}
11010
11011 %\appendix
11012 %\chapter{A Brief Guide to Basic Notions and Terminology}
11013
11014 %\section{Algebra}\label{sec:app-algebra}
11015 %
11016 %An algebra is a system of sets and operators thereon, usually written as a
11017 %list $(S_1, \ldots, S_n, f_1, \ldots, f_m)$. Typical examples are
11018 %a monoid $(M,e,*)$ and a vector space $(V,\cdot)$ over a field $F$.
11019 %
11020 %When there is only one underlying set,
11021 %the rank of an operator is just a natural number that indicates the number
11022 %of arguments.
11023 %For example, in the above monoid, $e$ has rank zero and $*$ has rank $2$.
11024 %If more than one sets are involved, the rank has to be described more
11025 %carefully. In general the rank of an operator is written as
11026 %$f_1 : S_1 \times \ldots \times S_l \to S$ where $S_i$ is the $i$-th
11027 %projection of the domain, and $S$ is the codomain.
11028 %
11029 %The operators of an algebra are often characterised by
11030 %a equational theory. In the case of the above monoid, $e$ and $*$ are
11031 %characterised by three ``law''s
11032 %\begin{quotation}
11033 %$\forall x,y,z[x * (y * z) = (x * y) * z]$ \\
11034 %$\forall x[x * e = x]$ \\
11035 %$\forall x[e * x = x]$
11036 %\end{quotation}
11037 %where the variables are understood to range over $M$. In case of
11038 %the above vector space, $\cdot : F \times V \to V$ is characterised
11039 %by
11040 %\begin{quotation}
11041 %$\forall x \in F,v,w \in V[x \cdot (v + w) = x \cdot v + x \cdot w]$ \\
11042 %$\forall x,y \in F,v \in V[(x + y) \cdot v = x \cdot v + y \cdot v]$ \\
11043 %$\forall x,y \in F,v \in V[(xy)\cdot v = x \cdot (y \cdot v)]$ \\
11044 %$\forall v \in V[1 \cdot v = 1]$ \\
11045 %\end{quotation}
11046 %where $+$ etc. are operators of the underyling field $F$ or group $V$.
11047 %
11048 %Some operator is not total. For example, the inverse $\_^{-1}$ of
11049 %a field is undefined on the additive neutral $0$. Hence
11050 %the characterisation of an operator requires a statement on which elements
11051 %of the domain it is defined on.
11052 %
11053 %A graph is an algebra $(V,E,s,t)$, where
11054 %$s$, $t$ have the same rank $E \to V$ with no characteristic equations.
11055 %You may enrich the algebra with the set of paths $P$ and the composition
11056 %$\circ : P P -> P$ and the extensions to $s$, $t$ to $P$, characterised
11057 %by
11058 %\begin{quotation}
11059 %$\forall e \in E[e \in P]$ \\
11060 %For all $p,q \in P$, $p \circ q$ is defined iff $sp = tq$ \\
11061 %$\forall p,q \in P[s(p \circ q) = sq]$ (if defined) \\
11062 %$\forall p,q \in P[t(p \circ q) = tp]$ (if defined)
11063 %\end{quotation}
11064 %
11065 %\section{Category}\label{sec:app-category}
11066 %
11067 %A category is an algebra $(Obj,Arr,dom,cod,id,\circ)$ where
11068 %$(Obj,Arr,dom,cod)$ is a graph.
11069 %
11070 %A functor from a category $(Obj,Arr,dom,cod,id,\circ)$ to another
11071 %$(Obj',Arr',dom',cod',id',\circ')$ is a pair of functions.
11072 %
11073 %A natural transformation from a functor to another is
11074 %a function.
11075 %
11076 %\section{Institution}\label{sec:app-institution}
11077 %
11078 %An institution is a triple $(S,M,\models)$.
11079 %
11080 %\section{Coinduction}\label{sec:app-coinduction}
11081 %
11082 %A coinduction is a proof technique that uses bisimulation.
11083
11084 \begin{thebibliography}{?-}
11085
11086 \bibitem{trs-handbook} Dershowitz,~N. and Jouannaud,~J.-P.,
11087 ``Rewrite Systems'',
11088 \textit{Handbook of Theoretical Computer Science, Vol.B: Formal Models
11089 and Semantics}, The MIT Press/Elsevier Science Publishers,
11090 1990, pp.245--320
11091
11092 \bibitem{razvan96-1} Diaconescu,~R. and Futatsugi,~K.,
11093 \textit{Logical Semantics of CafeOBJ}, Technical Report
11094 IS-RR-96-0024S, Japan Advanced Institute for Science and
11095 Teleology, 1996
11096
11097 \bibitem{cafeobj-rep} Diaconescu,~R. and Futatsugi,~K.,
11098 \textit{CafeOBJ Report}, World Scientific, 1998
11099
11100 \bibitem{e-m85} Ehrig,~H. and Mahr,~B.,
11101 \textit{Fundamentals of Algebraic Specifications 1:
11102 Equations and Initial Semantics},
11103 Springer-Verlag, 1985
11104
11105 \bibitem{institution} Goguen,~J. and Burstall,~R.,
11106 ``Institutions: Abstract Model Theory for Specification and Programming'',
11107 \textit{Journal of the Association for Computing Machinery},
11108 Vol.39, 1992, pp.95--146
11109
11110 \bibitem{osa-survey} Goguen,~J. and Diaconescu,~R.,
11111 ``An Oxford Survey of Order Sorted Algebra'',
11112 Mathematical Structures in Computer Science, Vol.4, 1994,
11113 pp.363--392
11114
11115 \bibitem{hsa} Goguen,~J. and Malcom,~G.,
11116 \textit{A Hidden Agenda}, technical report, UCSD, 1998
11117
11118 \bibitem{osa} Goguen,~J.A. and Meseguer,~J.,
11119 \textit{Order-Sorted Algebra 1:
11120 Equational Deduction for Multiple Inheritance, Polymorphism,
11121 Overloading and Partial Operations},
11122 Technical Report SRI-CSL-89-10, SRI International, 1989
11123
11124 \bibitem{OBJ} Goguen,~J., Winkler,~T., Meseguer,~J., Futatsugi,~K.,
11125 and Jouannaud,~J.-P.,
11126 \textit{Introducing OBJ}, Technichal Report
11127 SRI-CSL-92-03, SRI International, 1992
11128
11129 \bibitem{eatcs-coalg} Jacobs,~B. and Rutten,~J.,
11130 ``A Tutorial on (Co)Algebras and (Co)Induction'',
11131 \textit{EATCS Bulletin}, No.62, EATCS, 1997, pp.222--259
11132
11133 \bibitem{trs-eatcs} Klop,~J.W.,
11134 ``Term Rewriting Systems: A Tutorial'',
11135 \textit{EATCS Bulletin}, No.32, EATCS, 1987, pp.143--182
11136
11137 \bibitem{rew-logic} Meseguer,~J.,
11138 ``Conditional Rewriting Logic: Deduction, Models and Concurrency'',
11139 \textit{Proc. 2nd International CTRS Workshop}, Lecture Notes in
11140 Computer Science 516, 1991, pp.64--91
11141
11142 \bibitem{m-g82} Meseguer,J. and Goguen,J.A.,
11143 ``Initiality, induction and computability'',
11144 \textit{Algebraic Methods in Semantics},
11145 Cambridge University Press, 1984, pp.459--541
11146
11147 \end{thebibliography}
11148 \clearpage
11149 \twocolindex
11150 \pagestyle{index}
11151 %\renewcommand{\chaptermark}[1]{}
11152 \renewcommand{\preindexhook}{%
11153 The first page number is usually, but not always, the primary reference to
11154 the indexed topic.\vskip\onelineskip}
11155 \indexintoc
11156
11157 %%%\raggedright does nasty things to index entries
11158 \printindex
11159
11160 \onecolindex
11161 \renewcommand*{\preindexhook}{}
11162 \renewcommand*{\indexname}{Index of first lines}
11163 %%%\indexintoc
11164 \printindex[lines]
11165
11166 \cleardoublepage
11167
11168 \end{document}
11169
11170 %%% Local Variables:
11171 %%% mode: latex
11172 %%% TeX-master: t
11173 %%% End: