| 1 |
Overview of the system |
| 2 |
====================== |
| 3 |
|
| 4 |
Let us start with a simple definition of a module, which are the basic |
| 5 |
building blocks of any CafeOBJ program: |
| 6 |
````` |
| 7 |
mod NATPAIR { |
| 8 |
pr(NAT) |
| 9 |
[Pair] |
| 10 |
var P : Pair |
| 11 |
op <_,_> : Nat Nat -> Pair {constr} |
| 12 |
op fst : Pair -> Nat |
| 13 |
op snd : Pair -> Nat |
| 14 |
eq fst( < A:Nat , B:Nat > ) = A . |
| 15 |
eq snd( < A:Nat , B:Nat > ) = B . |
| 16 |
} |
| 17 |
````` |
| 18 |
This example already presents most of the core concepts of CafeOBJ: |
| 19 |
|
| 20 |
* modules as the basic building blocks |
| 21 |
* import of other modules `pr(NAT)` |
| 22 |
* sorts `[Pair]` |
| 23 |
* operator signature and equations |
| 24 |
|
| 25 |
Let us start with sorts, as they are the fundamental types. |
| 26 |
|
| 27 |
Sorts |
| 28 |
----- |
| 29 |
|
| 30 |
Most programming languages allow for different sorts, or types of |
| 31 |
objects. In this respect CafeOBJ is not different and allows to have |
| 32 |
arbitrary sorts. In addition, these sorts can be ordered, more |
| 33 |
specific one sort can be declared a sub-sort of another. In the above |
| 34 |
example |
| 35 |
|
| 36 |
````` |
| 37 |
[ Pair ] |
| 38 |
````` |
| 39 |
|
| 40 |
a new sort called `Pair` is introduced. This is a completely new sort |
| 41 |
and is in no sub-sort relation to any other sort. This is a very |
| 42 |
common case, and reflects the different types of objects in other |
| 43 |
programming languages. |
| 44 |
|
| 45 |
In case one wants to introduce ordering in the sorts, the order can be |
| 46 |
expressed together with the definition of the sort, as in: |
| 47 |
````` |
| 48 |
[ Nat < Set ] |
| 49 |
````` |
| 50 |
which would introduce a new sort `Set` and declares it as supersort of |
| 51 |
the (builtin) sort `Nat`. |
| 52 |
|
| 53 |
For more details concerning sorts, see [`sort declaration`](#sort). |
| 54 |
|
| 55 |
Imports {#intro-imports} |
| 56 |
------- |
| 57 |
|
| 58 |
CafeOBJ allows for importing and reusing of already defined |
| 59 |
modules: |
| 60 |
````` |
| 61 |
pr(NAT) |
| 62 |
````` |
| 63 |
for example pulls in the natural numbers (in a very minimal |
| 64 |
implementation). There are several modes of pulling in other modules, |
| 65 |
differing in the way the (semantic) models of the included module are |
| 66 |
treated. |
| 67 |
|
| 68 |
After a statement of import, the sorts, variables, and operators of |
| 69 |
the imported modules can be used. |
| 70 |
|
| 71 |
For more details see [`protecting`](#protecting), |
| 72 |
[`extending`](#extending), [`using`](#using), [`including`](#including) |
| 73 |
|
| 74 |
|
| 75 |
Variables and Operators |
| 76 |
----------------------- |
| 77 |
|
| 78 |
While sorts define data types, variables hold objects of a specific |
| 79 |
type, and operators define functionality. For each variable its sort |
| 80 |
has to be declared, and for each operator the signature, i.e., the |
| 81 |
sorts of the input data and the sort of the output, has to be given. |
| 82 |
````` |
| 83 |
var P : Pair |
| 84 |
op fst : Pair -> Nat |
| 85 |
````` |
| 86 |
This example declares a variable `P` of type pair, and an operator |
| 87 |
`fst` which maps the sort `Pair` to the sort `Nat`, or in other words, |
| 88 |
a function that maps pairs of natural numbers to natural numbers. |
| 89 |
|
| 90 |
We have seen already a different way to specify operators, namely |
| 91 |
````` |
| 92 |
op <_,_> : Nat Nat -> Pair {constr} |
| 93 |
````` |
| 94 |
which introduces an infix operator. CafeOBJ is very flexible and |
| 95 |
allows to freely specify the syntax. In an operator declaration as the |
| 96 |
above, the underscores `_` represent arguments to the operator. That |
| 97 |
also means that the number of underscores must match the number of |
| 98 |
sorts given before the `->`. After the above declaration CafeOBJ |
| 99 |
will be able to parse terms like `< 3 , 4 >` and correctly type them |
| 100 |
as pair. |
| 101 |
|
| 102 |
For further details, see [`var`](#var), [`op`](#op). |
| 103 |
|
| 104 |
|
| 105 |
Equations (or Axioms) |
| 106 |
--------------------- |
| 107 |
Using sorts, variables, and operators we have specified the terms that |
| 108 |
we want to speak about. In the following equations, or sometimes |
| 109 |
called axioms, will equate different terms. Equating here is meant in |
| 110 |
the algebraic sense, but also in the term-rewriting sense, as |
| 111 |
equations form the basis of rewrite rules which provide CafeOBJ with |
| 112 |
the executable semantics: |
| 113 |
````` |
| 114 |
eq fst( < A:Nat , B:Nat > ) = A . |
| 115 |
eq snd( < A:Nat , B:Nat > ) = B . |
| 116 |
````` |
| 117 |
As soon as an operator like `fst` has been declared, we can give |
| 118 |
equations. In this case we define `fst` of a pair to return the first |
| 119 |
element. |
| 120 |
|
| 121 |
For further details see [`eq`](#eq). |
| 122 |
|
| 123 |
******************* |
| 124 |
|
| 125 |
In the following chapter we will include the specification of a |
| 126 |
protocol with the full code, explaining some concepts on the way. |
| 127 |
|