/
FamilyTree.elm
628 lines (568 loc) · 25.3 KB
/
FamilyTree.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
module FamilyTree exposing (..)
import Css
import Datalog exposing (Database)
import Datalog.Database as Database
import Dict exposing (Dict)
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attrs exposing (css)
import Html.Styled.Events as Events
import Set
type alias Model =
{ db : Database
, nextId : Int
-- transient view state
, newPersonField : String
, lastError : Maybe Datalog.Problem
, parentId : Maybe Int
, childId : Maybe Int
, activePerson : Maybe Int
}
type Msg
= UserTypedInNewPersonField String
| UserClickedAddPerson
| UserChoseParent (Maybe Int)
| UserChoseChild (Maybe Int)
| UserClickedAddParentChildRelationship
| UserClickedShowPerson Int
| UserClickedLoadSampleData Dataset
type Dataset
= KennedyFamily
init : ( Model, Cmd Msg )
init =
let
dbResult =
Datalog.empty
|> Datalog.register "person" [ Database.IntType, Database.StringType ]
|> Result.andThen (Datalog.register "parent" [ Database.IntType, Database.IntType ])
in
( { db =
case dbResult of
Ok db ->
db
Err problem ->
Datalog.empty
, nextId = 0
, newPersonField = ""
, lastError =
case dbResult of
Ok _ ->
Nothing
Err problem ->
Just problem
, parentId = Nothing
, childId = Nothing
, activePerson = Nothing
}
, Cmd.none
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UserTypedInNewPersonField input ->
( { model | newPersonField = input }
, Cmd.none
)
UserClickedAddPerson ->
case
Datalog.insert "person"
[ Datalog.int model.nextId
, Datalog.string model.newPersonField
]
model.db
of
Ok newDb ->
( { model
| db = newDb
, nextId = model.nextId + 1
, newPersonField = ""
}
, Cmd.none
)
Err problem ->
( { model | lastError = Just problem }
, Cmd.none
)
UserChoseParent parentId ->
( { model | parentId = parentId }
, Cmd.none
)
UserChoseChild childId ->
( { model | childId = childId }
, Cmd.none
)
UserClickedAddParentChildRelationship ->
case ( model.parentId, model.childId ) of
( Just parentId, Just childId ) ->
case
Datalog.insert "parent"
[ Datalog.int parentId
, Datalog.int childId
]
model.db
of
Ok newDb ->
( { model
| db = newDb
, parentId = Nothing
, childId = Nothing
}
, Cmd.none
)
Err problem ->
( { model | lastError = Just problem }
, Cmd.none
)
_ ->
( model
, Cmd.none
)
UserClickedShowPerson id ->
( { model | activePerson = Just id }
, Cmd.none
)
UserClickedLoadSampleData dataset ->
( loadSampleData dataset
, Cmd.none
)
view : Model -> Html Msg
view model =
let
allPeople =
Datalog.read "person" personDecoder model.db
in
Html.div
[]
[ Html.h1 [] [ Html.text "Family Tree" ]
, Html.p [] [ Html.text "Enter names for the family tree below. Once you've added at least two names, you can set parent/child relationships. Click names you've already entered to see the relationships between that person and other family members." ]
, Html.p [] [ Html.text "Alternatively, you can load a pre-made family tree:" ]
, Html.ul []
[ Html.li []
[ linkButton (UserClickedLoadSampleData KennedyFamily) "Kennedy Family (U.S. Political Dynasty)"
, Html.text " (Note: this is a HUGE family, and so if you know about the Kennedys you'll notice people missing. But it's enough for a demo!)"
]
]
, Html.p []
[ Html.text "View the Elm source for this page at "
, Html.a
[ Attrs.href "https://git.bytes.zone/brian/bad-datalog/src/branch/main/sample-apps/src/FamilyTree.elm" ]
[ Html.text "sample-apps/src/FamilyTree.elm" ]
, Html.text " in the "
, Html.a
[ Attrs.href "https://git.bytes.zone/brian/bad-datalog" ]
[ Html.text "brian/bad-datalog repo" ]
, Html.text "."
]
, case model.lastError of
Nothing ->
Html.text ""
Just problem ->
viewError problem
, Html.div
[ css
[ Css.displayFlex
, Css.justifyContent Css.spaceAround
]
]
[ viewAddNewPersonForm model
, viewResult
(\_ -> Html.text "")
(viewAddParentChildForm model)
allPeople
]
, Html.div
[ css [ Css.displayFlex ] ]
[ Html.section [ css [ Css.flexGrow (Css.int 1) ] ]
[ Html.h2 [] [ Html.text "People" ]
, viewResult viewError (viewPeopleList model) allPeople
]
, case model.activePerson of
Nothing ->
Html.text ""
Just personId ->
viewRelationships model personId
]
]
viewAddNewPersonForm : Model -> Html Msg
viewAddNewPersonForm model =
Html.section []
[ Html.h2 [] [ Html.text "Add a New Person" ]
, viewForm
{ onSubmit = UserClickedAddPerson
, fields =
[ ( "Name"
, Html.input
[ Attrs.value model.newPersonField
, Events.onInput UserTypedInNewPersonField
]
[]
)
]
, submitCaption = "Add Person"
}
]
viewAddParentChildForm : Model -> List Person -> Html Msg
viewAddParentChildForm model people =
let
peopleOptions =
people
|> List.map
(\{ id, name } ->
Html.option
[ Attrs.value (String.fromInt id) ]
[ Html.text name ]
)
|> (::)
(Html.option
[ Attrs.value "" ]
[ Html.text "---" ]
)
in
Html.section []
[ Html.h2 [] [ Html.text "Add a Parent/Child Relationship" ]
, case people of
_ :: _ :: _ ->
viewForm
{ onSubmit = UserClickedAddParentChildRelationship
, fields =
[ ( "Parent"
, Html.select
[ Events.onInput (UserChoseParent << String.toInt)
, model.parentId
|> Maybe.map String.fromInt
|> Maybe.withDefault ""
|> Attrs.value
, css [ Css.flexGrow (Css.int 1) ]
]
peopleOptions
)
, ( "Child"
, Html.select
[ Events.onInput (UserChoseChild << String.toInt)
, model.childId
|> Maybe.map String.fromInt
|> Maybe.withDefault ""
|> Attrs.value
, css [ Css.flexGrow (Css.int 1) ]
]
peopleOptions
)
]
, submitCaption = "Add parent/child relationship"
}
_ ->
Html.text "add at least two people and I'll let you set relationships!"
]
type alias Person =
{ id : Int
, name : String
}
personDecoder : Datalog.Decoder Person
personDecoder =
Datalog.into Person
|> Datalog.intField 0
|> Datalog.stringField 1
viewPeopleList : Model -> List Person -> Html Msg
viewPeopleList model people =
case people of
[] ->
Html.text "Nobody!"
_ ->
Html.div []
[ people
|> List.map
(\person ->
Html.li []
[ Html.text person.name
, Html.text " ("
, linkButton (UserClickedShowPerson person.id) "Show relationships"
, Html.text ")"
]
)
|> Html.ul []
]
viewRelationships : Model -> Int -> Html Msg
viewRelationships model personId =
let
derived =
Datalog.derive
[ {- me(id, name) :- person(id, name), id = personId -}
Datalog.rule "me"
[ "id", "name" ]
[ Datalog.with "person" [ Datalog.var "id", Datalog.var "name" ]
, Datalog.filter (Datalog.eq "id" (Datalog.int personId))
]
, {- parents(id, name) :- person(id, name), parent(id, personId). -}
Datalog.rule "parents"
[ "id", "name" ]
[ Datalog.with "person" [ Datalog.var "id", Datalog.var "name" ]
, Datalog.with "parent" [ Datalog.var "id", Datalog.int personId ]
]
, {- children(id, name) :- person(id, name), parent(personId, id). -}
Datalog.rule "children"
[ "id", "name" ]
[ Datalog.with "person" [ Datalog.var "id", Datalog.var "name" ]
, Datalog.with "parent" [ Datalog.int personId, Datalog.var "id" ]
]
, {-
siblings(siblingId, siblingName) :-
person(siblingId, siblingName),
parent(parentId, personId),
parent(parentId, siblingId),
siblingId /= personId.
-}
Datalog.rule "siblings"
[ "siblingId", "siblingName" ]
[ Datalog.with "person" [ Datalog.var "siblingId", Datalog.var "siblingName" ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.int personId ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.var "siblingId" ]
, Datalog.filter (Datalog.not_ (Datalog.eq "siblingId" (Datalog.int personId)))
]
, {-
grandparents(grandparentId, grandparentName) :-
person(grandparentId, grandparentName),
person(parentId, personId),
person(grandparentId, parentId).
-}
Datalog.rule "grandparents"
[ "grandparentId", "grandparentName" ]
[ Datalog.with "person" [ Datalog.var "grandparentId", Datalog.var "grandparentName" ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.int personId ]
, Datalog.with "parent" [ Datalog.var "grandparentId", Datalog.var "parentId" ]
]
, Datalog.rule "grandchildren"
[ "grandchildId", "grandchildName" ]
[ Datalog.with "person" [ Datalog.var "grandchildId", Datalog.var "grandchildName" ]
, Datalog.with "parent" [ Datalog.var "childId", Datalog.var "grandchildId" ]
, Datalog.with "parent" [ Datalog.int personId, Datalog.var "childId" ]
]
, {-
auntsAndUncles(auId, auName) :-
person(auId, auName),
parent(grandparentId, auId),
parent(grandparentId, parentId),
parent(parentId, personId),
auId /= parentId.
-}
Datalog.rule "auntsAndUncles"
[ "auId", "auName" ]
[ Datalog.with "person" [ Datalog.var "auId", Datalog.var "auName" ]
, Datalog.with "parent" [ Datalog.var "grandparentId", Datalog.var "auId" ]
, Datalog.with "parent" [ Datalog.var "grandparentId", Datalog.var "parentId" ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.int personId ]
, Datalog.filter (Datalog.not_ (Datalog.eq "auId" (Datalog.var "parentId")))
]
, Datalog.rule "niecesAndNephews"
[ "nnId", "nnName" ]
[ Datalog.with "person" [ Datalog.var "nnId", Datalog.var "nnName" ]
, Datalog.with "parent" [ Datalog.var "siblingId", Datalog.var "nnId" ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.var "siblingId" ]
, Datalog.with "parent" [ Datalog.var "parentId", Datalog.int personId ]
, Datalog.filter (Datalog.not_ (Datalog.eq "siblingId" (Datalog.int personId)))
]
]
model.db
viewSection : String -> List Person -> Html Msg
viewSection name people =
Html.section []
[ Html.h3 [] [ Html.text name ]
, viewPeopleList model people
]
in
derived
|> Result.andThen (Datalog.readOne "me" personDecoder)
|> Result.andThen
(\me ->
flattenResults
[ Ok (Html.h2 [] [ Html.text me.name ])
, derived
|> Result.andThen (Datalog.read "parents" personDecoder)
|> Result.map (viewSection "Parents")
, derived
|> Result.andThen (Datalog.read "grandparents" personDecoder)
|> Result.map (viewSection "Grandparents")
, derived
|> Result.andThen (Datalog.read "auntsAndUncles" personDecoder)
|> Result.map (viewSection "Aunts and Uncles")
, derived
|> Result.andThen (Datalog.read "siblings" personDecoder)
|> Result.map (viewSection "Siblings")
, derived
|> Result.andThen (Datalog.read "children" personDecoder)
|> Result.map (viewSection "Children")
, derived
|> Result.andThen (Datalog.read "niecesAndNephews" personDecoder)
|> Result.map (viewSection "Nieces and Nephews")
, derived
|> Result.andThen (Datalog.read "grandchildren" personDecoder)
|> Result.map (viewSection "Grandchildren")
]
)
|> viewResult viewError (Html.div [ css [ Css.flexGrow (Css.int 1) ] ])
viewError : Datalog.Problem -> Html msg
viewError problem =
Html.details
[]
[ Html.summary [] [ Html.text "Something went wrong. Oh no! Here are more details." ]
, Html.text (Debug.toString problem)
]
viewResult : (error -> Html msg) -> (success -> Html msg) -> Result error success -> Html msg
viewResult viewError_ viewSuccess result =
case result of
Ok success ->
viewSuccess success
Err error ->
viewError_ error
viewForm :
{ onSubmit : msg
, fields : List ( String, Html msg )
, submitCaption : String
}
-> Html msg
viewForm { onSubmit, fields, submitCaption } =
Html.form
[ Events.onSubmit onSubmit ]
(List.map
(\( label, input ) ->
Html.label
[ css
[ Css.displayFlex
, Css.marginBottom (Css.px 10)
]
]
[ Html.span [ css [ Css.minWidth (Css.px 75) ] ] [ Html.text label ]
, input
]
)
fields
++ [ Html.button
[ css [ Css.width (Css.pct 100) ] ]
[ Html.text submitCaption ]
]
)
flattenResults : List (Result x a) -> Result x (List a)
flattenResults =
List.foldr (Result.map2 (::)) (Ok [])
linkButton : msg -> String -> Html msg
linkButton msg caption =
Html.button
[ Events.onClick msg
, css
[ Css.fontSize Css.unset
, Css.border Css.zero
, Css.backgroundColor Css.unset
, Css.display Css.inline
, Css.margin Css.unset
, Css.padding Css.unset
, Css.color (Css.hex "006064")
, Css.textDecoration Css.underline
, Css.cursor Css.pointer
]
]
[ Html.text caption ]
loadSampleData : Dataset -> Model
loadSampleData dataset =
let
addPerson :
Int
-> String
-> Result Datalog.Problem Datalog.Database
-> Result Datalog.Problem Datalog.Database
addPerson id name =
Result.andThen (Datalog.insert "person" [ Datalog.int id, Datalog.string name ])
in
case dataset of
KennedyFamily ->
let
familyTree : Dict String (List String)
familyTree =
Dict.fromList
[ ( "Joseph P 'Joe' Kennedy Sr.", [ "Joseph Patrick Kennedy Jr.", "John Fitzgerald 'Jack' Kennedy", "Rose Marie Kennedy", "Kathleen Agnes (Marchioness of Hartington) Kennedy", "Eunice Mary Kennedy", "Patricia Helen Kennedy", "Robert Francis 'Bobby' Kennedy", "Jean Ann Kennedy", "Edward Moore 'Ted' Kennedy" ] )
, ( "Joseph P 'Joe' Kennedy Sr.", [ "Joseph Patrick Kennedy Jr.", "John Fitzgerald 'Jack' Kennedy", "Rose Marie Kennedy", "Kathleen Agnes (Marchioness of Hartington) Kennedy", "Eunice Mary Kennedy", "Patricia Helen Kennedy", "Robert Francis 'Bobby' Kennedy", "Jean Ann Kennedy", "Edward Moore 'Ted' Kennedy" ] )
, ( "John Fitzgerald 'Jack' Kennedy", [ "Arabella Kennedy", "Caroline Bouvier Kennedy", "John Fitzgerald Kennedy Jr.", "Patrick Bouvier Kennedy" ] )
, ( "Jacqueline Lee Bouvier", [ "Arabella Kennedy", "Caroline Bouvier Kennedy", "John Fitzgerald Kennedy Jr.", "Patrick Bouvier Kennedy" ] )
, ( "Eunice Mary Kennedy", [ "Robert Sargent Shriver III", "Maria Owings Shriver", "Timothy Perry Shriver", "Mark Kennedy Shriver", "Anthony Paul Kennedy Shriver" ] )
, ( "Robert Sargent Shriver Jr.", [ "Robert Sargent Shriver III", "Maria Owings Shriver", "Timothy Perry Shriver", "Mark Kennedy Shriver", "Anthony Paul Kennedy Shriver" ] )
, ( "Patricia Helen Kennedy", [ "Christopher Kennedy Lawford", "Sydney Maleaia Kennedy Lawford", "Victoria Francis Lawford", "Robin Elizabeth Lawford" ] )
, ( "Peter Sydney Ernest Lawford", [ "Christopher Kennedy Lawford", "Sydney Maleaia Kennedy Lawford", "Victoria Francis Lawford", "Robin Elizabeth Lawford" ] )
, ( "Robert Francis 'Bobby' Kennedy", [ "Kathleen Hartington Kennedy", "Joseph Patrick Kennedy II", "Robert Francis Kennedy Jr.", "David Anthony Kennedy", "Mary Courtney Kennedy", "Michael LeMoyne Kennedy", "Mary Kerry 'Kerry' Kennedy", "Christopher George Kennedy", "Matthew Maxwell Taylor 'Max' Kennedy", "Douglass Harriman Kennedy", "Rory Elizabeth Katherine Kennedy" ] )
, ( "Ethel Skakel", [ "Kathleen Hartington Kennedy", "Joseph Patrick Kennedy II", "Robert Francis Kennedy Jr.", "David Anthony Kennedy", "Mary Courtney Kennedy", "Michael LeMoyne Kennedy", "Mary Kerry 'Kerry' Kennedy", "Christopher George Kennedy", "Matthew Maxwell Taylor 'Max' Kennedy", "Douglass Harriman Kennedy", "Rory Elizabeth Katherine Kennedy" ] )
, ( "Jean Ann Kennedy", [ "Stephen Edward Smith Jr.", "William Kennedy Smith", "Amanda Mary Smith", "Kym Maria Smith" ] )
, ( "Stephen Edward Smith", [ "Stephen Edward Smith Jr.", "William Kennedy Smith", "Amanda Mary Smith", "Kym Maria Smith" ] )
, ( "Edward Moore 'Ted' Kennedy", [ "Kara Anne Kennedy", "Edward Moore Kennedy Jr.", "Patrick Joseph Kennedy II" ] )
, ( "Virginia Joan Bennet", [ "Kara Anne Kennedy", "Edward Moore Kennedy Jr.", "Patrick Joseph Kennedy II" ] )
, ( "Maria Owings Shriver", [ "Katherine Eunice Schwarzenegger", "Christina Maria Aurelia Schwarzenegger", "Patrick Arnold Shriver Schwarzenegger", "Christopher Sargent Shriver Schwarzenegger" ] )
, ( "Arnold Alois Schwarzenegger", [ "Katherine Eunice Schwarzenegger", "Christina Maria Aurelia Schwarzenegger", "Patrick Arnold Shriver Schwarzenegger", "Christopher Sargent Shriver Schwarzenegger" ] )
, ( "Katherine Eunice Schwarzenegger", [ "Lyla Maria Pratt" ] )
, ( "Christopher Michael Pratt", [ "Lyla Maria Pratt" ] )
]
people : Dict String Int
people =
familyTree
|> Dict.foldl
(\parent children soFar ->
soFar
|> Set.insert parent
|> Set.union (Set.fromList children)
)
Set.empty
|> Set.toList
|> List.indexedMap (\i person -> ( person, i ))
|> Dict.fromList
parents : List ( Int, Int )
parents =
familyTree
|> Dict.toList
|> List.concatMap (\( parent, children ) -> List.map (Tuple.pair parent) children)
|> List.filterMap
(\( parent, child ) ->
Maybe.map2 Tuple.pair
(Dict.get parent people)
(Dict.get child people)
)
dbResult =
Ok Datalog.empty
|> (\dbResultWithoutPeople ->
Dict.foldl
(\person id ->
Result.andThen
(Datalog.insert "person"
[ Datalog.int id
, Datalog.string person
]
)
)
dbResultWithoutPeople
people
)
|> (\dbResultWithoutParents ->
List.foldl
(\( parent, child ) ->
Result.andThen
(Datalog.insert "parent"
[ Datalog.int parent
, Datalog.int child
]
)
)
dbResultWithoutParents
parents
)
in
{ db =
case dbResult of
Ok db ->
db
Err problem ->
Datalog.empty
, nextId = 0
, newPersonField = ""
, lastError =
case dbResult of
Ok _ ->
Nothing
Err problem ->
Just problem
, parentId = Nothing
, childId = Nothing
, activePerson = Dict.get "John Fitzgerald 'Jack' Kennedy" people
}