Simple supertype, complex subtype

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Simple supertype, complex subtype

Post by Chris Walton » Sat Aug 24, 2013 4:42 pm

If it is possible to express the following situation in Rel, could you suggest the syntax to achieve it?

These have been defined:
TYPE identifier
POSSREP { VALUE CHAR CONSTRAINT
LENGTH ( VALUE ) <= 36 } ;
VAR BDT BASE RELATION
{ name identifier }
KEY { name } ;
TYPE base_data_type IS
{ identifier
CONSTRAINT TUPLE { name identifier } IN BDT { name }
POSSREP { X = THE_VALUE ( identifier ) } } ;

and a number of subtypes of base_data_type have been defined - all of the form:
VAR Arbitrary BASE RELATION
{ name identifier }
KEY { name } ;
TYPE arbitrary IS
{ base_data_type
CONSTRAINT TUPLE { name identifier ( base_data_type ) } IN Arbitrary { name }
POSSREP { Y = THE_X ( base_data_type ) } } ;

base_data_type is a very simple data type - the only data it has is its name.

I now want to define a further data type, also a subtype of base_data_type, but which is more complex; and all the complexity resides in the subtype. If it were not a subtype it would be defined as:
TYPE timestamp
POSSREP ISO {
YY N99,
MM N12 } ;

where:
TYPE N99
POSSREP { VALUE INTEGER CONSTRAINT
VALUE >= 00 AND VALUE <= 99 } ;TYPE N31
POSSREP { VALUE INTEGER CONSTRAINT
VALUE >= 01 AND VALUE <= 31 } ;

All my attempts at defining this as a subtype in a similar way to the other subtypes [with a corresponding relvar defined:
VAR Timestamp BASE RELATION
{ name identifier,
CC N99,
MM N12 }
KEY { name };]
lead to messages like "N99 is not a supertype of timestamp".

What is the appropriate syntax for this definition?

Dave
Site Admin
Posts: 364
Joined: Sun Nov 27, 2005 7:19 pm

Re: Simple supertype, complex subtype

Post by Dave » Sat Aug 24, 2013 9:55 pm

I'm not quite sure I understand what you want to do. Can you describe your requirement in a bit more detail?

What statement results in "N99 is not a supertype of timestamp"?

Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Re: Simple supertype, complex subtype

Post by Chris Walton » Mon Aug 26, 2013 9:55 am

I am trying to define data types within a type system. The data type supertype is called base_data_type. This has a number of subtypes, such as arbitrary, symbolic, etc. One of these subtypes is time related - test_date below. This subtype can be represented as YYMM. So the components of test_date are YY - year, and MM - month. Each data type definition of a subtype also becomes an tuple in a relvar listing all the instances of the subtype.

I have defined:
Utility types:
TYPE identifier POSSREP { VALUE CHAR CONSTRAINT LENGTH ( VALUE ) <= 36 } ;
TYPE N99 POSSREP { VALUE INTEGER CONSTRAINT VALUE >= 00 AND VALUE <= 99 } ;
TYPE N12 POSSREP { VALUE INTEGER CONSTRAINT VALUE >= 01 AND VALUE <= 12 } ;
The supertype:
TYPE base_data_type POSSREP { VALUE base_data_type };
The associated relvar:
VAR BDT BASE RELATION { name base_data_type } KEY { name } ;

I now want to define test_date. I need to be able to go on, to define operators on test_date of the form minus(P test_date, Q test_date). These operators have no meaning in the context of base_data_type.
The associated relvar is:
VAR Test_date BASE RELATION { name base_data_type } KEY { name } ;
Some of my various attempts to define test_date have been:
1) TYPE test_date IS { base_data_type CONSTRAINT TUPLE { name base_data_type } IN Test_date { name } POSSREP ISO { X = THE_VALUE ( base_data_type ), YY N99, MM N12 } } ;
which gives rise to: ERROR: Encountered "N99" at line 9, column 10. Was expecting: "=" ...
2) TYPE test_date IS { base_data_type CONSTRAINT TUPLE { name base_data_type } IN Test_date { name } POSSREP ISO { X = THE_VALUE ( base_data_type ), YY = THE_VALUE ( N99 ), MM = THE_VALUE ( N12 ) } } ;
which gives rise to: ERROR: Type 'N99' is not a supertype of 'test'. Line 7
3) TYPE test IS { base_data_type CONSTRAINT TUPLE { name base_data_type } IN Test_date { name } POSSREP ISO { THE_VALUE(base_data_type), YY N99, MM N12 } } ;
which gives rise to: ERROR: Encountered "(" at line 6, column 22. Was expecting: "=" ...

I have already defined a number of subtypes of base_data_type which have no additional attributes. These all have the form:
TYPE arbitrary IS { base_data_type CONSTRAINT TUPLE { name base_data_type } IN Arbitrary { name } POSSREP { X = THE_VALUE ( base_data_type ) } } ;
with associated relvar:
VAR Arbitrary BASE RELATION { name base_data_type } KEY { name } ;

Dave
Site Admin
Posts: 364
Joined: Sun Nov 27, 2005 7:19 pm

Re: Simple supertype, complex subtype

Post by Dave » Mon Aug 26, 2013 10:34 am

Maybe specialisation-by-constraint isn't what you want here. Perhaps try a simple structural-inheritance hierarchy? Similar to this:

Code: Select all

TYPE basetype UNION;

TYPE derivedtype1 IS {basetype POSSREP {x INTEGER, y CHAR}};

TYPE derivedtype2 IS {basetype POSSREP {x INTEGER, y CHAR}};

TYPE derivedtype3 IS {derivedtype1 POSSREP {a RATIONAL}};
Would something like that work?

Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Re: Simple supertype, complex subtype

Post by Chris Walton » Tue Aug 27, 2013 2:34 pm

Thanks - that does exactly what I am trying to do. It is amazing how obscure the simple solution can be.

Post Reply