on request_timeout help sign in
project-m36/docs/new_datatypes.markdown
51 lines 2.8 kB history raw
1# Creating New Data Types
3## Introduction
5The most common data types are built into Project:M36 directly. Such types include integers, text, byte strings, booleans and floats. It can often be handy to create one's own data types in a database and manipulate them as first-class values through the relational algebra.
7In addition, the Project:M36 runtime supports algebraic data types in the same vein as Haskell itself.
9Project:M36 makes it easy to add any Haskell data type to the database. Any type which includes instances for a set of typeclasses can be supported.
11## Support for Runtime Types
13Use the ```tutd``` interpreter to create new type and data constructors with essentially the same syntax as in Haskell:
15```
16TutorialD (master/main): data Hair = Bald | Brown | Blond | OtherColor Text
17TutorialD (master/main): :showexpr relation{tuple{name "Steve", hair Blond}, tuple{name "Sam", hair OtherColor "Grey"}}
18┌─────────────────┬──────────┐
19│hair::Hair │name::Text│
20├─────────────────┼──────────┤
21│Blond │"Steve" │
22│OtherColor "Grey"│"Sam" │
23└─────────────────┴──────────┘
24```
26SQL's enumerations or even joins do not compare favorably to a genuine algebraic data type. Exercise for the reader: why is this? Do you agree?
28Types can be forgotten as well:
30```
31TutorialD (master/main): undata Hair
32```
34These runtime types can therefore change between transactions. Note that the underlying data values do not change when the types change. Once changed, only new values will reflect the new data constructors.
36The downside to this approach is that functions which operate on these runtime types cannot be written in the TutorialD interpreter (yet).
38Note that the Haskell-standard "Maybe a" and "Either a b" data types are included by default.
40## Support for Haskell Types
42Any Haskell data type which is an instance of ```Atomable``` is a candidate for a Project:M36 value called an ```Atom```. Database tuples are composed of Atoms.
44Project:M36 does not yet support dynamically-loaded components, so the data type must be compiled into the Project:M36 backend.
46### Implementation for Haskell Types
481. Create an instance of your data type for the ```Atomable``` typeclass. The ```Atomable``` typeclass implies instances for a variety of standard typeclasses.
491. Update the ```Binary Atom``` instance to include deserialization for your type.
501. Update the ```makeAtomFromText``` in ```ProjectM36/Atom.hs``` to support CSV import.
511. Optionally adjust ```TutorialD/Interpreter/RelationalExpression.hs``` to add support for parsing your data type in the TutorialD interpreter. Look for ```atomP```.