Next: Every function is a method
Up: The ALLCOT semantics
Previous: Categories
Basic principles.
- Each domain D1 has a category C1
and must implement every function from C1.
- Every element from D1 is encoded in the same way
and this encoding is given by the
AttribStatement.
- We call attribute each of the formal parameters
in an
AttribStatement.
- The
AttribStatement is mandatory in every domain
definition but its list of attributes may be empty.
- If a domain C2 inherits from a domain C1
then the attributes of C2 are those of C1
followed by those declared in the
AttribStatement
of C2.
- In a domain C2 inheriting from a domain C1,
if a function f is defined in both C1 and C2,
then the version of C2 is used.
- In a domain C2 inheriting from a domain C1,
if a function f is declared for both C1 and C2
but is only implemented in C1, then two cases arise:
- if C1 and C2 have the same attributes, then
the function f defined in C1 can be reused
to implement the C2-function f just by making a call
(with a possible cast). See the example below.
- if C2 has some additional attributes, then in order to
be in the previous case, the target program needs to define
a map from C2 to C1.
When translated to C, a domain D1 will become a struct
definition followed by function definitions.
For instance, consider the following ALLCOT program.
CoupleType: Category == with {
construct: (Integer, Integer) -> %;
equal: (%, %) -> Integer;
print: (%) -> ();
}
Couple: CoupleType == add {
attrib(left: Integer, right: Integer);
construct(l: Integer, r: Integer): % == attrib(l, r);
equal(x: %, y: %): Integer == {
if (x.left = y.left) and (x.right = y.right)
then {return 2; } else {return 0; }
}
print(x: %): () == {
print(x.left);
print(x.right);
}
}
Pair: CoupleType == Couple add {
attrib();
construct(l: Integer, r: Integer): % ==
if (l = r) then error else attrib(l, r);
equal(x: %, y: %): Integer == {
if ((x.left = y.left) and (x.right = y.right) or
(x.left = y.right) and (x.right = y.left))
then {return (2); }
else {return (0);}
}
}
main():() == {
p: Couple := construct(1,2);
print(p);
q: Pair := construct(2,1);
print(p);
}
main();
A possible translation to C would be
struct Couple
{
int left;
int right;
};
struct Couple *construct_Couple(int l, int r)
{
struct Couple *tmp_Couple = (struct Couple *) malloc(sizeof(struct Couple));
(tmp_Couple) -> left = l;
(tmp_Couple) -> right = r;
return (tmp_Couple);
};
int equal_Couple(struct Couple *tmp1_Couple, struct Couple *tmp2_Couple)
{
if (((tmp1_Couple) -> left == (tmp1_Couple) -> left) &&
((tmp1_Couple) -> right == (tmp1_Couple) -> right)) {
return (2);
} else {
return (1);
}
}
int print_Couple(struct Couple *tmp_Couple)
{
int tmp_int_0 = 0;
printf("%d \n", (tmp_Couple) -> left );
printf("%d \n", (tmp_Couple) -> right);
return (tmp_int_0);
};
struct Pair
{
int left;
int right;
};
struct Pair *construct_Pair(int l, int r)
{
struct Pair *tmp_Pair = (struct Pair *) malloc(sizeof(struct Pair));
if (l == r) {
exit(1);
} else {
(tmp_Pair) -> left = l;
(tmp_Pair) -> right = r;
return (tmp_Pair);
}
};
int equal_Pair(struct Pair *tmp1_Pair, struct Pair *tmp2_Pair)
{
if ((((tmp1_Pair) -> left == (tmp2_Pair) -> left) &&
((tmp1_Pair) -> right == (tmp2_Pair) -> right)) ||
(((tmp1_Pair) -> left == (tmp2_Pair) -> right) &&
((tmp1_Pair) -> right == (tmp2_Pair) -> left))) {
return (2);
} else {
return (0);
}
}
int print_Pair(struct Pair *tmp_Pair)
{
return (print_Couple((struct Couple *) tmp_Pair));
};
int main()
{
int tmp_int_1 = 1;
int tmp_int_2 = 2;
int tmp_int_3 = 2;
int tmp_int_4 = 1;
struct Couple *p = construct_Couple(tmp_int_1, tmp_int_2);
struct Pair *q = construct_Pair(tmp_int_3, tmp_int_4);
print_Couple(p);
print_Pair(q);
return 0;
};
Next: Every function is a method
Up: The ALLCOT semantics
Previous: Categories
Marc Moreno Maza
2004-12-01