on 62_python_dbapi help sign in
project-m36/docs/new_datatypes.markdown
69 lines 3.3 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
42New algebraic data types can be added to the database context at any time via TutorialD or Haskell. Typeclasses for database types are not supported.
44### Implementation for Haskell Types
46Given a ```ProjectM36.Client``` ```Connection```, create an ```AddTypeConstructor``` with arguments a) type constructor name and b) data constructors.
48Example:
49```haskell
50AddTypeConstructor (ADTypeConstructorDef "Hair" []) [DataConstructorDef "Brown" [], DataConstructorDef "Blond" [], DataConstructorDef "Bald" [],...]
51```
53Pass the ```AddTypeConstructor``` value to ```executeDatabaseContextExpr``` to add it to the database.
55To create a value for the new algebraic data type, create the type:
57```haskell
58let hairType = ConstructedAtomType "Hair" M.empty
59```
61then create the value:
63```haskell
64let blondAtom = ConstructedAtom "Blond" hairType []
65```
67The ```blondAtom``` can now be used in any place where an Atom is otherwise used such as in ```AtomExpr``` checking for equality in restriction or as an argument to ```AtomFunction```s or simply in building a new relation.
69In the previous example, the additional empty arguments are for polymorphic variables and arguments. In this simple ADT, there is no polymorphism necessary, so the arguments are empty.