Skip to content

Commit

Permalink
Family Age Gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidPratten committed Sep 24, 2023
1 parent 10b9d37 commit ca1a921
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ The Jetisu Toolkit is exploring the possibility that the relational model can do

<img width="100%" src="images/doing for rules what rm did for data.png">

## 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)
Expand Down
66 changes: 66 additions & 0 deletions puzzles/family_age_gaps.md
Original file line number Diff line number Diff line change
@@ -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;

```

0 comments on commit ca1a921

Please sign in to comment.