Trouble with TYPE

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
aurelie
Posts: 2
Joined: Thu Nov 05, 2009 11:05 pm

Trouble with TYPE

Post by aurelie »

I have some trouble with the use of TYPE in the version 2.03 of Rel-DBrowser.
When I try to define a type like this:

TYPE Telephone POSSREP {tel CHAR CONSTRAINT SUBSTR(tel,1,1)='+' AND CHAR_LENGTH(tel)>2 AND CHAR_LENGTH(tel)<17};

Rel says that it's OK but then if I try to use this type or if i just want to delete it, Rel says that the type has not been defined.

I'm not sure if it's a bug or not, maybe I am doing something wrong.
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

Sounds like you're using an old version of Rel, probably one before TYPE support was complete. The current version of Rel is 0.3.17 and DBrowser is 2.07.
erkie
Posts: 2
Joined: Thu Apr 15, 2010 12:24 pm

Re: Trouble with TYPE

Post by erkie »

I have some trouble with the use of TYPE in the rel version 0.3.20

I try to execute a command:

TYPE POINT
POSSREP CARTESIAN {X RATIONAL, Y RATIONAL}
POSSREP POLAR {R RATIONAL, THETA RATIONAL};

The system replies:
ERROR: POSSREP 'CARTESIAN' has no INITialisation.

It was possible to execute the statement in an earlier Rel version.

Then I try the use of initialization.

TYPE Point
POSSREP cartesian {X RATIONAL, Y RATIONAL}
POSSREP polar {R RATIONAL, THETA RATIONAL}
INIT cartesian(X := 0.0, Y := 0.0);

The system replies:
ERROR: Component 'Y' cannot be assigned because it is set via the selector.

Is it possible to define types with multiple possible representation in Rel? If yes, then what is the correct syntax?
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

Try this:

Code: Select all

TYPE Point
   POSSREP cartesian {X RATIONAL, Y RATIONAL}
   POSSREP polar {R RATIONAL, THETA RATIONAL}
INIT 
   cartesian (R := SQRT(X * X + Y * Y), THETA := ATAN(Y / X))
   polar (X := R * COS(THETA), Y := R * SIN(THETA));
The INIT section defines each POSSREP in terms of the value being selected. If you select a cartesian Point, it initialises the polar POSSREP from the cartesian Point. If you select a polar Point, it initialises the cartesian POSSREP from the polar Point.

NOTE: The above example depends on the OperatorsMath.d script having been loaded and executed from the Scripts directory. It defines SQRT, ATAN, COS, SIN, and many other math operators.
erkie
Posts: 2
Joined: Thu Apr 15, 2010 12:24 pm

Re: Trouble with TYPE

Post by erkie »

The Third Manifesto (Third Edition) book does not seem to require INIT clause in case of scalar types.

Therefore I have a question - is it a bug or a feature?

If it is a feature, hen how should I define a type with for instance three different possible representations?
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

The book describes the type model, but leaves out some of the low-level details. Essentially, it treats type definitions as interfaces without specifying how the interfaces are implemented. Page 382 of TTM 3rd ed. refers to "highly protected operators [that are] not part of D" that might be needed to implement a TYPE definition. For simplicity's sake, Rel uses INIT in place of these. In effect, this means a type's actual and possible representation are the same, but it simplifies implementing typical type definitions. Eventually, more sophisticated type definition facilities -- i.e., a mechanism to write "highly protected operators [that are] not part of D" -- will be provided.

So, in short, INIT is neither a bug nor a feature. It's a workaround. :)

Here's an example of a type with three POSSREPs:

Code: Select all

TYPE Temperature
  POSSREP Celsius {celsius RATIONAL}
  POSSREP Kelvin {kelvin RATIONAL}
  POSSREP Fahrenheit {fahrenheit RATIONAL}
INIT
  Celsius (kelvin := celsius + 273.15, fahrenheit := (9.0 / 5.0) * celsius + 32.0)
  Kelvin (celsius := kelvin - 273.15, fahrenheit := ((kelvin - 273.15) * (9.0 / 5.0)) + 32.0)
  Fahrenheit (kelvin := (5.0 / 9.0 * (fahrenheit - 32.0) + 273.15), celsius := (5.0 / 9.0) * (fahrenheit - 32.0));
aurelie
Posts: 2
Joined: Thu Nov 05, 2009 11:05 pm

Re: Trouble with TYPE

Post by aurelie »

I'm trying to use the syntax with INIT that you propose but the problem is I have to init a Point that is another user-define type.
I'm working with DBrowser 2.08

Here is the definition of Point:
TYPE Point POSSREP Point{Abscisse RATIONAL,Ordonne RATIONAL};

Here is the definition of a line:
TYPE Line
POSSREP Line2p{p1 Point,p2 Point}
POSSREP LineInfinite{SETOF Point}
INIT Line2p( p1:=Point(0,0), p2:=Point(0,0));

This syntax returns the following error :
ERROR: Component 'p2' cannot be assigned because it is set via the selector.

Is there a way to do what I want ?
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

aurelie wrote:Is there a way to do what I want ?
Yes. Your approach using two POSSREPs in one type won't work because Line2p and LineInfinite are not equivalent. What I think you want is this:

TYPE Line UNION;
TYPE Line2p IS {Line POSSREP {p1 Point, p2 Point}};
TYPE LineInfinite IS {Line POSSREP {SETOF Point}};

Of course, the above presumes the existence of SETOF, which is not available in Rel.

However, this would work:

Code: Select all

TYPE Line UNION;
TYPE Line2p IS {Line POSSREP {p1 Point, p2 Point}};
TYPE LineInfinite IS {Line POSSREP {points RELATION {p Point}}};
katoussa
Posts: 11
Joined: Wed May 05, 2010 10:24 pm

Re: Trouble with TYPE

Post by katoussa »

I have some trouble with the use of TYPE POINT.
I define the TYPE POINT:

TYPE Point POSSREP Point {Abscisse RATIONAL,Ordonne RATIONAL};

I have a relation Person with a type POINT:
VAR Personnes REAL RELATION {Id integer, Nom char, Point Point} KEY {Id};

I have a problem on the insertion Person:

INSERT Personnes RELATION {
TUPLE{Id 1, Nom 'Personne1', Point (10,70)}

};

How can I make tuple's insertion?

Thanks.
katoussa
Posts: 11
Joined: Wed May 05, 2010 10:24 pm

Re: Trouble with TYPE

Post by katoussa »

Dave wrote:
aurelie wrote:Is there a way to do what I want ?
Yes. Your approach using two POSSREPs in one type won't work because Line2p and LineInfinite are not equivalent. What I think you want is this:

TYPE Line UNION;
TYPE Line2p IS {Line POSSREP {p1 Point, p2 Point}};
TYPE LineInfinite IS {Line POSSREP {SETOF Point}};

Of course, the above presumes the existence of SETOF, which is not available in Rel.

However, this would work:

Code: Select all

TYPE Line UNION;
TYPE Line2p IS {Line POSSREP {p1 Point, p2 Point}};
TYPE LineInfinite IS {Line POSSREP {points RELATION {p Point}}};

How we can make insertion of tuples with type Line2p and LineInfinite?
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

katoussa wrote: I have a problem on the insertion Person:

INSERT Personnes RELATION {
TUPLE{Id 1, Nom 'Personne1', Point (10,70)}

};

How can I make tuple's insertion?
You're missing the 'Point' attribute name in the TUPLE. It should be:

Code: Select all

INSERT Personnes RELATION {
    TUPLE {Id 1, Nom 'Personne1', Point Point(10.0, 70.0)}    
};
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Trouble with TYPE

Post by Dave »

katoussa wrote: How we can make insertion of tuples with type Line2p and LineInfinite?
Assuming the following relvar exists:

Code: Select all

VAR Personnes REAL RELATION {Id integer, Nom char, Line Line} KEY {Id};
The following will work:

Code: Select all

INSERT Personnes RELATION {
	TUPLE {Id 1, Nom 'Personne1', Line Line2p(Point(10.0, 70.0), Point(2.0, 5.0))}
};

INSERT Personnes RELATION {
        TUPLE {
                   Id 2, 
                   Nom 'Personne2', 
                   Line LineInfinite(
                         RELATION {
                             TUPLE {p Point(10.0, 70.0)}, 
                             TUPLE {p Point(2.0, 5.0)}
                         }
                   )
        }
};
Post Reply