diff --git a/README.md b/README.md index 3555559..3b73cd5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ The Jetisu Toolkit is exploring the possibility that the relational model can do -## Rules As Code examples +## Puzzle Examples +- [Family Age Gaps](puzzles/family_age_gaps.md) + +## Rules As Code Examples The examples are chosen to illustrate the benefits (and/or challenges) of using sigma complete relations to model rules as code. - [ACT Conveyance Duty](ACT_Conveyance_Duty.ipynb) - [Australian GST](Australian_GST.ipynb) diff --git a/puzzles/family_age_gaps.md b/puzzles/family_age_gaps.md new file mode 100644 index 0000000..9381b37 --- /dev/null +++ b/puzzles/family_age_gaps.md @@ -0,0 +1,66 @@ +# How old is Donald? + +The following puzzle is from Peter Revesz's 1993 paper "A closed-form evaluation for Datalog queries with integer (gap)-order constraints." _Theoretical Computer Science, 116(1), 117–149._ https://doi.org/10.1016/0304-3975(93)90222-F + +> Alfred and Alice have children Bernard, Carl and Donald in this order. +Bernard and Bernice have a child Edward. Donald and Denise have children Elise and +Fred. Edward and Elise have a child, Gerald. Fred and Felice have a child, Harold. +Alfred is not yet 70, and Harold is already in school. Gerald was born just last +month. Elise is over four years older than her younger brother Fred, and Bernard is over +25 years older than his son Edward. Assuming that there is over 17 years difference between parents and their children +and that no siblings are twins or born in the same year, how old is Donald? + +And here is a literate programming solution using a complete relation and SQL. + +```SQL +SELECT Donald_age +from COMPLETE( + Alfred_age int, Alice_age int, Bernard_age int, + Bernice_age int, Carl_age int, Donald_age int, + Denise_age int, Edward_age int, Elise_age int, + Fred_age int, Felice_age int, Gerald_age int, + Harold_age int + ) +WHERE + ---- Approach + ---- Given that no siblings are twins or born in the same year we will use > rather than >= + ---- Given the age gap between parents and their children we will add a generational gap test for each family + + -- Alfred and Alice have children Bernard, Carl and Donald in this order. + Bernard_age > Carl_age + AND Carl_age > Donald_age + -- generational gap of >17 years + AND min(Alfred_age, Alice_age) - 17 > max(Bernard_age, Bernice_age, Carl_age, Donald_age) + + -- Bernard and Bernice have child Edward. + -- generational gap of >17 years + AND min(Bernard_age, Bernice_age) - 17 > Edward_age + + -- Donald and Denise have children Elise and Fred. + -- generational gap of >17 years + AND min(Donald_age, Denise_age) - 17 > max(Elise_age, Fred_age) + + -- Edward and Elise have child Gerald. + -- generational gap of >17 years + AND min(Edward_age, Elise_age) - 17 > Gerald_age + + -- Fred and Felice have child Harold + -- generational gap of >17 years + AND min(Fred_age, Felice_age) - 17 > Harold_age + + -- Alfred is not yet 70 + AND Alfred_age < 70 + + -- Harold is already in school + AND Harold_age > 5 + + -- Gerald was born just last month + AND Gerald_age = 0 + + -- Elise is over 4 years older than her younger brother Fred + AND Elise_age - 4 > Fred_age + + -- Bernard is over 25 years older than his son Edward + AND Bernard_age - 25 > Edward_age; + +```