Skip to content

Instantly share code, notes, and snippets.

@drdozer
Last active August 29, 2015 14:15
Show Gist options
  • Save drdozer/5b76df6534f2bae88d54 to your computer and use it in GitHub Desktop.
Save drdozer/5b76df6534f2bae88d54 to your computer and use it in GitHub Desktop.
xsd built-in data types in scala - a sketch
trait XsdSimpleTypes {
type anySimpleType
type string
type duration
type dateTime
type time
type date
type gYearMonth
type gYear
type gMonthDay
type gDay
type gMonth
type boolean
type base64Binary
type hexBinary
type float
type decimal
type double
type anyURI
type QName
type NOTATION
type normalizedString
type token
type language
type Name
type NMTOKEN
type NCName
type ID
type IDREF
type ENTITY
type IDREFS
type ENTITIES
type integer
type nonPositiveInteger
type long
type nonNegativeInteger
type negativeInteger
type int
type short
type byte
type unsignedLong
type positiveInteger
type unsignedInt
type unsignedShort
type unsignedByte
}
trait XsdSimpleTypesHiearachy[XSD <: XsdSimpleTypes] {
def derivedFrom_anySimpleType: XSD#anySimpleType >:~> Coproduct.`
XSD#string,
XSD#duration,
XSD#dateTime,
XSD#time,
XSD#date,
XSD#gYearMonth,
XSD#gYear,
XSD#gMonthDay,
XSD#gDay,
XSD#gMonth,
XSD#boolean,
XSD#base64Binary,
XSD#hexBinary,
XSD#float,
XSD#decimal,
XSD#double,
XSD#anyURI,
XSD#QName,
XSD#NOTATION`.T
def derivedFrom_string:
XSD#string >:~> XSD#normalizedString
def derivedFrom_normalizedString:
XSD#normalizedString >:~> XSD#token
def derivedFrom_token:
XSD#token >:~> Coproduct.`XSD#language, XSD#Name, XSD#NMTOKEN)`.T
def derivedFrom_Name:
XSD#Name >:~> XSD#NCName
def derivedFrom_NCName:
XSD#NCName >:~> Coproduct.`XSD#ID, XSD#IDREF, XSD#ENTITY`.T
def listOfIDREF:
XSD#IDREF >_*> XSD#IDREFS
def listOfENTITY:
XSD#ENTITY >_*> XSD#ENTITIES
def derivedFrom_decimal:
XSD#decimal >:~> XSD#integer
def derivedFrom_integer:
XSD#integer >:~> Coproduct.`XSD#nonPositiveInteger, XSD#long, XSD#nonNegativeInteger`.T
def derivedFrom_nonPositiveInteger:
XSD#nonPositiveInteger >:~> XSD#negativeInteger
def derivedFrom_long:
XSD#long >:~> XSD#int
def derivedFrom_int:
XSD#int >:~> XSD#short
def derivedFrom_short:
XSD#short >:~> XSD#byte
def derivedFrom_nonNegativeInteger:
XSD#nonNegativeInteger >:~> Coproduct.`XSD#unsignedLong, XSD#positiveInteger`.T
def derivedFrom_unsignedLong:
XSD#unsignedLong >:~> XSD#unsignedInt
def derivedFrom_unsignedInt:
XSD#unsignedInt >:~> XSD#unsignedShort
def derivedFrom_unsignedShort:
XSD#unsignedShort >:~> XSD#unsignedByte
}
@drdozer
Copy link
Author

drdozer commented Feb 22, 2015

Notes:

T >:~> X witnesses that T has members that can be transformed naturally into X. This 'smells like' sub-typing. (T >:~> X)#sealed witnesses that every T can be transformed naturally into X, as in a sealed type hierarchy.

When X is a single type, it expresses that some instances of T can be viewed as instances of X. When X is a coproduct like Coproduct.```A, B```.T, it witnesses that some X can be viewed as instances A and some others as B. This is not a covering axiom. That is, there may be instances of T that can't be expressed as an X. For example, xsd#decimal >:~> xsd#integer expresses that some decimals can be viewed as integers. However, some decimals have values outside the range of integers.

Given T >:~>X, a natural fold can be derived, were there is a case for each type in X and a fall-through of type T for values that could not be cast to any of the specific types. This makes T >:~> _ composable as a type-level monoid.

(T >:~> X) |+| (T >:~> Y) ---> T >:~> (X |+| Y) were if X and Y are both coproducts, X |+| Y is Coproduct.ExtendBy, and if either are not, they are lifted in as singleton coproducts first.

@drdozer
Copy link
Author

drdozer commented Feb 23, 2015

Notes:

The type T >_*> X witnesses that X is derived from T as a list.

In the xsd serialized literal form, this will be values matching the serialized literal form of T separated by whitespace. In the scala data space, this will most likely map down to some collection type. However, it may map down to tuples or even a case class, in situations were that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment