Ex 10 A type system
This example is a reasonable sized system to illustrate many aspects of the syntax, particularly as it relates to data types and operators. The attached archive contains the following three files:
script.txt  to generate the complete type system
Why.txt  a full description of why another type system has been developed although Rel has its own
Behaviour.txt  the expected behaviour of type systems
This example type system is believed to be internally consistent, and compatible with TTM strictures, though this has not been proved. It is internally documented. The type system is not complete  it is short on needed operators, and is only a skeleton as regards the values of the respective base data types.
Any comments, corrections, observations, criticisms, or compliments will be very welcome, and useful.
Ex 10 A type system
Re: Ex 10 A type system
Good stuff. I wonder if you'd consider posting the contents of the ZIP file as a post, here, or perhaps even better as three separate posts? That would make it more convenient to quote, copy, or refer to specific items in responses.

 Posts:76
 Joined:Sat Aug 18, 2012 2:13 pm
Re: Ex 10 A type system
OK  I'll put the script in this thread; the expectations in language discussions; and the rationale as a separate thread.

 Posts:76
 Joined:Sat Aug 18, 2012 2:13 pm
Ex10 Type definition system
EDITED 20131117 incorporating substantial refinements of the system.
Code: Select all
//
// Bootstrap
//
OPERATOR LENGTH(s CHAR) RETURNS INTEGER Java FOREIGN
return ValueInteger.select(context.getGenerator(), s.stringValue().length());
END OPERATOR;
TYPE ID POSSREP { VALUE CHARACTER CONSTRAINT LENGTH ( VALUE ) <= 36 };
TYPE nonneg_int POSSREP { VALUE INTEGER CONSTRAINT ( VALUE >= 0 ) };
TYPE nonneg_real POSSREP { VALUE RATIONAL CONSTRAINT ( VALUE >= 0.0 ) };
TYPE pos_int POSSREP { VALUE INTEGER CONSTRAINT ( VALUE > 0 ) };
TYPE pos_real POSSREP { VALUE RATIONAL CONSTRAINT ( VALUE > 0.0 ) };
OPERATOR CHARACTER(P ID) RETURNS CHARACTER; return THE_VALUE ( P ) ; END OPERATOR ;
//
// Method Formalism
//
TYPE data_type UNION POSSREP { domain ID, name ID, T ID };
VAR data_types BASE RELATION {
domain ID, name ID, T ID, predicate RELATION { O1 CHARACTER } } KEY { domain, name, T };
VAR duration_units BASE RELATION { name ID, sort# nonneg_int } KEY { sort# };
VAR sign_values BASE RELATION { name ID } KEY { name };
VAR symbolic_formats BASE RELATION { name ID } KEY { name };
VAR whitespace_fillers BASE RELATION { name ID } KEY { name };
TYPE duration_unit ORDERED IS { ID CONSTRAINT TUPLE { name ID } IN duration_units { name }
POSSREP { X = THE_VALUE ( ID ) } };
TYPE magnitude POSSREP { VALUE INTEGER CONSTRAINT VALUE >= 35 AND VALUE <= 27 };
TYPE symb_format IS { ID CONSTRAINT TUPLE { name ID } IN symbolic_formats { name }
POSSREP { X = THE_VALUE ( ID ) } };
TYPE sign_value IS { ID CONSTRAINT TUPLE { name ID } IN sign_values { name }
POSSREP { X= THE_VALUE ( ID ) } };
TYPE whitespace_filler IS { ID CONSTRAINT TUPLE { name ID } IN whitespace_fillers { name }
POSSREP { X = THE_VALUE ( ID ) } };
VAR arbitrary_defns BASE RELATION { T data_type } KEY { T };
VAR boolean_defns BASE RELATION { T data_type } KEY { T };
VAR duration_defns BASE RELATION {
T data_type,
low_limit RATIONAL,
high_limit RATIONAL,
units duration_unit,
is_unbounded boolean,
precision magnitude }
KEY { T };
VAR enumeration_defns BASE RELATION {
T data_type,
values RELATION { C1 ID } }
KEY { T };
VAR extended_boolean_defns BASE RELATION {
T data_type,
values RELATION { C1 ID } }
KEY { T };
VAR numeric_defns BASE RELATION {
T data_type,
number_base pos_int,
low_limit RATIONAL,
high_limit RATIONAL,
precision magnitude,
units ID,
is_unbounded boolean }
KEY { T };
VAR ordered_defns BASE RELATION {
T data_type,
values RELATION { C1 ID } }
KEY { T };
VAR partial_order_defns BASE RELATION {
T data_type,
values RELATION { C1 ID } }
KEY { T };
VAR symbolic_defns BASE RELATION {
T data_type,
min_length RELATION { O1 nonneg_int },
length nonneg_int,
is_case_sensitive BOOLEAN,
format symb_format,
whitespace whitespace_filler }
KEY { T };
VAR timestamp_defns BASE RELATION {
T data_type,
precision duration_unit }
KEY { T };
CONSTRAINT numeric_defn_C1 AND ( numeric_defns, low_limit <= high_limit );
CONSTRAINT symbolic_defn_C1 WITH ( A := symbolic_defns ) :
AND ( A, COUNT ( A { min_length } ) <= 1 );
CONSTRAINT symbolic_defn_C2 WITH ( A := symbolic_defns UNGROUP ( min_length ) ) :
AND ( A, O1 <= length );
duration_units := RELATION {
TUPLE { name ID ( "millisecond" ), sort# nonneg_int ( 3 ) },
TUPLE { name ID ( "second" ), sort# nonneg_int ( 4 ) },
TUPLE { name ID ( "minute" ), sort# nonneg_int ( 5 ) },
TUPLE { name ID ( "hour" ), sort# nonneg_int ( 6 ) },
TUPLE { name ID ( "day" ), sort# nonneg_int ( 7 ) },
TUPLE { name ID ( "week" ), sort# nonneg_int ( 8 ) },
TUPLE { name ID ( "month" ), sort# nonneg_int ( 9 ) },
TUPLE { name ID ( "year" ), sort# nonneg_int ( 10 ) },
TUPLE { name ID ( "century" ), sort# nonneg_int ( 11 ) } };
sign_values := RELATION {
TUPLE { name ID ( "+" ) },
TUPLE { name ID ( "" ) },
TUPLE { name ID ( "" ) } };
symbolic_formats := RELATION {
TUPLE { name ID ( "aS iS" ) },
TUPLE { name ID ( "CamelCased" ) },
TUPLE { name ID ( "lower cased" ) },
TUPLE { name ID ( "pascalCased" ) },
TUPLE { name ID ( "Sentence cased" ) },
TUPLE { name ID ( "UPPER CASED" ) } };
whitespace_fillers := RELATION {
TUPLE { name ID ( " " ) },
TUPLE { name ID ( "_" ) },
TUPLE { name ID ( "" ) } };
INSERT Type_predicate RELATION { TUPLE {
name ID ( "magnitude" ),
Predicate "This is the power of 10 that describes the precision of a numeric value. Values range from 35 (ie 10^35, the Planck distance) to 27 (10^27, the size of the observable universe)." } };
INSERT Type_predicate RELATION { TUPLE {
name ID ( "data_type" ),
Predicate "Data types are defined as part of the method.\nThere is no explicit type casting available (for the sole exception to this rule see operator 'as base').\nData types are based on a two level scheme:\nDomainspecific data types are defined by the analyst in order to capture#eas such as power, voltage, position, temperature, etc. for a particular domain. The analyst defines an appropriate set of domainspecific data types for the domain, stating a name for each data type and other pertinent such as precision, range and the like.\n\nA domainspecific data type is defined by reference to an appropriate base data type. A data item of a particular type must not be used in a context where a different type is expected. To do so is considered to be a compile time error. A type defines the range of values permitted, and the operations permitted on that range." } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "arbitrary_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "A data type to represent arbitrary IDs.\n\nUsage: type <name> is arbitrary.\n\nThe implementation of an arbitrary type is determined by the architecture domain. The analyst should make no assumptions as to how this is done: the arbitrary type may be implemented as a handle, an integer, a character string, or by any other scheme." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "boolean_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "An enumerated data type with values true and false.\n\n Usage: type <type name> is boolean(default value is <value>).\n\nThe operators on this data type are comparison; = (equality) and <> (inequality); and the logical operators ¬, ^, and v (not, and, inclusive or). All the operators return values of type boolean." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "duration_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "A data type that represents duration.\n\nUsage: type <type name> is duration range is from <low limit> to <high limit> units are [century  year  month  day  hour  minute  second  millisecond] precision is < smallest discriminated value>.\n\n Example: type 'test window' is duration range is from 0 to 10 units are second precision is .001\n\nThe operators on time and duration are:\ntime := time +/ duration\nduration := duration * numeric\nduration := duration / numeric\nduration := time  time\nas well as the standard comparisons of <, >, <= and >= (read as before, after, before or equal to, and equal to or after). Each comparison returns a boolean. Comparisons are defined only between elements of the same type  time with time and duration with duration, but not time with duration." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "enumeration_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "Data type permitting a finite set of specified values.\n\nUsage: type <name> is enumerated values are <value1>, <value2>,...<valueN>\n\n Example: data type IC colour is enumerated values are red, black, blue, green, silver\n\n." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "extended_boolean_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "Enumerated data type with the values true, false, and noncomparable.\n\nUsage: data type <data type name> is extended boolean_\n\nThe operators on the type are:\n\nOR_F_T_N____AND_F_T_N_____NOT_F_____XOR_F_T_N\n_F_F_T_N_______F_F_F_F________F_T_______F_F_T_N\n_T_T_T_T_______T_F_T_N________T_F_______T_T_F_T\n_N_N_T_N_______N_F_N_N______N_N_____N_N_T_N\n\nThis data type is used in conjunction with partially ordered structures_\n\nThis type describes a three valued logic, but does not reintroduce nulls_ It is only used to describe partially ordered structures." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "numeric_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "A data type defining numeric values.\n\nUsage:\ndata type <data type name> is numeric (base <N>) range is from <low limit> to <high limit> units are <unit symbol> precision is <smallest discriminant>\nwhere base N specifies the base of the quantities <low limit>, <high limit>, <smallest discriminant>, and <value>. If base N is omitted, base 10 is assumed.\n\nExample:\ndata type ring diameter is numeric range is from 0 to 39 units are cm precision is 0.01\ndata type bit pattern is numeric base 8 range is from 0 to 177777 units are octal bits precision is 1\n\nNote that this definition does not specify whether a numeric data type will be implemented as an integer or a real number. This will be determined by the architecture, based on the native types available in the implementation language, the word length of these native types, and the range and precision required for the data type. The operators defined on the type are standard arithmetic operations +, , *(multiplication), /(division), %(division modulo N), and ^(exponentiation). These operators return type numeric. The standard arithmetic comparisons of =, <>, <, >, <=, >= result returns type boolean. The attribute is_unbounded is used when any of the numeric values, especially high_value, cannot be defined." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "ordered_type_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "The operators defined on an ordered data type are the comparisons: = (equal), <> (nonequal), < (before), > (after), <= (before or equal), >= (equal or after). Each comparison returns type boolean." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "partial_order_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "Partial_order types express order, such as first, second, and so on.\n\nUsage:\ndata type <data type name> is partial_order.\n\nThe operators defined for partial_order data types are:\n\nthe comparisons = and <> (equal and nonequal in value); and the comparisons < (before), >, <=, >=. These operators return extended booleans. A partial order is always applied to a set of elements. The set can be finite or infinite.\n\nThere are two types of ordering. The first is the most familiar; it is a complete ordering. This means you can express the concept of before (represented as <) between any two members of the set. A complete ordering has the property of transitivity: if A is before B, and B is before C, then A is before C.\n\nFar more interesting are the partial orderings. Consider this partial order.\n E => F => G\n ^\n l\nA => B => C => D.\n\nUsing the obvious interpretation we can say that A < B, C < D, C < E, and E < F. We cannot say anything about the relationship between D and F. They are noncomparable.\n\nExamples of structures that are partially ordered include PERT charts, trees used for any purpose, interlock chains, the connectivity of the electric grid and the like.\n\nWhen modelling such structures, it is often necessary to use ancillary object types an a significant set of relationships. Such constructs are of limited value, and can obscure the true purpose of the model. This is pertinent when modelling an architecture, where ordering is a prominent theme. The partial_order data type has been defined, to providing an alternative to using more fully expressive object types and relationships." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "symbolic_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "Used to type values that are names and descriptions.\n\nUsage:\ntype <name> is symbolic length is (from <min_length> to )<max_length>\nExample:\ntype 'gas name' is symbolic length is from 2 to 15 default value is Helium\n\nEquality and inequality operators are defined and return booleans. These operators depend on the attributes is_case_sensitive, strip_whitespace, and format; and on a collating sequence. A collating sequence prescribes the order of all the characters in a specified character set.\n\nWARNING: Case sensitive operators can imply that there are (nonapparent) duplicates in the database. These undermine the relational model, so use with care." } } } };
INSERT data_types RELATION { TUPLE {
domain ID ( "Method Formalism" ),
name ID ( "timestamp_defn" ),
T ID ( "data_type" ),
predicate RELATION { TUPLE {
O1 "Data type timestamp represents a moment in time.\n\nUsage:\n<name> is timestamp precision is <precision>.\nThe ISO representation, adopted as a standard, of a timestamp is: CCYYMMDDTHH:mm(:SS(.sss))([Z,[+,]HH:mm]) where\nCC is centuries;\nYY is years;\n, T, :, +, ., are literal dividers;\nMM is months;\nDD is days;\nHH is hours;\nmm is minutes;\nSS is seconds;\nsss is decimal fractions of a second;\nZ is the code for the deviation from USC. Z itself, indicates that the time is Universal Standard Time (UST) (= Greenwich Meantime), default value is Z.;\n+/HH:mm indicates the deviation from UST; until the alpha code is needed, only the +/HH:m usage is supported.\n\nBrackets indicate optional portions of the representation. Square brackets represent alternatives." } } } };