on master help sign in
WIP add out-of-the-tarpit-related docs and examples
commit489f43eda2dc45c07d92da8022d179f7b57a71d5 authorAgentM <agentm@themactionfaction.com> dateThu 24 Mar 2016, 21:55 (11 years ago) parentc79520b
2 files changed+137
docs/reaching_out_of_the_tarpit.markdown+70 examples/out_of_the_tarpit.tutd+67
docs/reaching_out_of_the_tarpit.markdownnew file+70
@@ -0,0 +1,70 @@
1+# Reaching Out of the Tarpit with Project:M36
2+
3+## Introduction
4+
5+[Project:M36](https://github.com/agentm/project-m36) is a relational algebra database management system (RDBMS) whose purpose is to implement the foundation for functional relational programming as described in the excellent ["Out of the Tarpit"](http://shaffner.us/cs/papers/tarpit.pdf) paper. This document describes how Project:M36 meets the design goals laid out in the paper. It is recommended to read the paper before reading this document.
6+
7+## Why is "Out of the Tarpit" Important?
8+
9+The "Out of the Tarpit" paper, written in 2006, consolidates decades of software development research and experience to explain why software written today is chock-full of software bugs. The paper categorizes sources of complexity and then proceeds to propose a software architecture which promises to reduce unnecessary software complexity so that the resultant software is maximally focused on actual business rules.
10+
11+## Laying the Foundations for Reduced Complexity
12+
13+After analyzing sources of software complexity (primarily unnecessarily mutable state and unintended order-of-execution), the not-so-radical proposal of the "Out of the Tarpit" paper is "functional-relational programming", not to be confused with "[functional reactive programming](https://wiki.haskell.org/Functional_Reactive_Programming)". According to the paper, the ideal language would minimize mutable state (such as in functional programming) and eliminate unintended order-of-execution by providing declarative instead of imperative constructs.
14+
15+The central software component of functional-relational programming is a relational algebra engine/database management system (RDBMS). Project:M36 purports to meet the software requirements of such a functional-relational system, perhaps being the first RDBMS to actually target the paper's proposed design.
16+
17+## The Importance of the Relational Model
18+
19+"Out of the Tarpit" recommends the relational model not only in its customary backend role, but throughout the application because the relational model is:
20+
21+* **declarative**: thus removing accidental complexity (such as accidental ordering)
22+* **restricted**: the paper makes it clear that if a programming language allows a programmer to do something less-than-savory, then that misfeature will be used
23+
24+and provides ([p.37](http://shaffner.us/cs/papers/tarpit.pdf)):
25+
26+* **structure**: a closed world system whereby all data is represented by relations
27+* **manipulation**: a means to simulate mutation of the application state over time
28+* **data integrity**: constraints which prevent illogical data constructions
29+* **data independence**: the logical and physical representations of state are clearly separated
30+
31+The relational model is a mathematical model for storing and manipulating "essential state" ([p.25](http://shaffner.us/cs/papers/tarpit.pdf)). As a mathematical model, it not beholden to any specific implementation or quirks.
32+
33+Unfortunately, due to the quirks of legacy implementations, much of what developers learn about the relational model is wrong. Project:M36 aims to rectify this situation by providing a mathematically coherent implementation of the relational model. Contrast the mathematical approach to existing databases which sacrifice or elide a mathematical model in lieu of "performance". Refer to [Knuth on premature optimization](https://en.wikiquote.org/wiki/Donald_Knuth) for additional details.
34+
35+## Chris Date's Contribution
36+
37+Chris Date is a prolific author, researcher, and lecturer on relation algebra and database design in general. Project:M36 would not exist without his writings on the mathematical underpinnings of the relational algebra. Project:M36 does not however implement all Date's recommendations. In particular, Project:M36 implements:
38+
39+* **algebraic data types** inspired by Haskell's types in lieu of Date's convoluted data types based on object-oriented type systems
40+* **transactions in a graph** in lieu of Date's temporal constructs
41+* **a TutorialD dialect** which is not 100% compatible with Date's proposal
42+* **isomorphic schemas** (not yet implemented) in lieu of Date's updateable views
43+
44+## Project:M36 In Practice
45+
46+Values which Project:M36 can store are typed by the powerful functional programming language Haskell. Any Haskell data value can become a database value by implementing a simple typeclass. Thus, it provides a native and seamless interface between Haskell and the relational algebra engine.
47+
48+Project:M36 does not, however, implement a completely new programming language for functional-relational programming, as suggested in the paper. The benefits of a fully-restricted programming language are described in the paper and this feature may be implemented in the future.
49+
50+Project:M36 implements multiple interfaces:
51+
52+* an interactive console implementing [TutorialD](https://en.wikipedia.org/wiki/D_%28data_language_specification%29)
53+* a native Haskell API
54+* a database driver for the [persistent framework](http://www.yesodweb.com/book/persistent)
55+
56+To understand how Project:M36 stacks up against the fictional programming language shown in the paper, we have implemented the example from the paper.
57+
58+feeders
59+observers
60+
61+## Good Riddance to Old Baggage
62+
63+Project:M36 is a clean-room implementation of a relational algebra engine; it does not implement SQL or any other legacy standard. While this means that Project:M36 is incompatible with other RDBMS, it means that Project:M36 can avoid the substantial mistakes of previous implementations, such as:
64+
65+* [three-valued logic (NULL)](docs/on_null.markdown)
66+* non-relational database constructs such as row and column ordering or OUTER JOINs
67+* linear transaction flow and conflict resolution
68+* direct table-to-file mapping which create perverse model-data dependencies
69+* key-value storage: a key-value mapping is valued merely for its performance characteristics, not because it models reality well
70+* restrictive, legacy type systems which impede modeling
examples/out_of_the_tarpit.tutdnew file+67
@@ -0,0 +1,67 @@
1+-- data type definitions
2+
3+data Address = Address Text
4+data Agent = Agent Text
5+data Name = Name Text
6+data Price = Price Double
7+data RoomType = Kitchen | Bathroom | LivingRoom
8+data PriceBand = Low | Medium | High | Premium
9+data AreaCode = City | Suburban | Rural
10+data SpeedBand = VeryFast | Fast | Medium | Slow
11+
12+-- relation variable definitions
13+
14+property := relation { address Address, price Price, photo Text, agent Agent, dateRegistered Day }
15+
16+offer := relation { address Address, offerPrice Price, offerDate Day, bidderName Name, bidderAddress Address, decisionDay Day, accepted Bool }
17+
18+decision := relation { address Address, offerDate Day, bidderName Name, bidderAddress Address, decisionDay Day, accepted Bool }
19+
20+room := relation { address Address, roomName Text, width Double, breadth Double, type RoomType }
21+
22+floor := relation { address Address, roomName Text, floor Int }
23+
24+commission := relation { priceBand PriceBand, areaCode AreaCode, saleSpeed SpeedBand, commission Double }
25+
26+-- uniqueness constraints
27+
28+key propertyKey {address} property
29+
30+key offerKey { address, offerDate, bidderName, bidderAddress } offer
31+
32+key decisionKey { address, offerDate, bidderName, bidderAddress } decision
33+
34+key roomKey { address, roomName } room
35+
36+key floorKey { address, roomName } floor
37+
38+key commissionKey { priceBand, areaCode, saleSpeed } commission
39+
40+-- foreign key constraints
41+foreign key offerPropertyFKey offer{ address } in property{ address }
42+
43+foreign key decisionOfferFKey offer{ address, offerDate, bidderName, bidderAddress } in decision{ address, offerDate, bidderName, bidderAddress }
44+
45+foreign key roomPropertyFKey room{ address } in property { address }
46+
47+foreign key floorPropertyFKey floor{ address } in property { address }
48+
49+-- The constraint which requires each property to have at least one room cannot be currently represented because it would conflict with the property/room foreign key (chicken and egg problem). Date makes it clear that constraints must hold at all times, not only when the transaction is committed. This is at odds with the OotT paper. Date's solution is the comma operator which is not yet implemented.
50+--constraint propertiesMustHaveARoom property
51+
52+constraint ownerIsNotBidder
53+
54+constraint noOffersAfterSale
55+
56+constraint noMoreThan50PremiumProperties
57+
58+constraint noBidderCanMakeMoreThan10BidsOnAProperty
59+-- sample data
60+
61+insert offer relation{tuple{address Address "Main St.", offerPrice Price 1400.0, bidderName "Jim", bidderAddress Address "345 Bidder St.", decisionDay fromGregorian(2015,4,4), accepted False}}
62+
63+insert property relation{tuple{address Address "Main St.", price Price 1000.0, photo "photo1.jpg", agent Agent "Bob", dateRegistered fromGregorian(2014, 1, 1)}}
64+
65+insert floor relation{tuple{address Address "Main St.", roomName "Bedroom", floor 3}}
66+
67+