/
DatalogTests.elm
609 lines (600 loc) · 30.4 KB
/
DatalogTests.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
module DatalogTests exposing (..)
import Datalog exposing (..)
import Datalog.Database as Database
import Expect exposing (Expectation)
import Test exposing (..)
datalogTests : Test
datalogTests =
describe "Datalog"
[ describe "planRule"
[ test "a simple read turns into a Read -> Project" <|
\_ ->
rule "mortal" [ "who" ]
|> with "greek" [ var "who" ]
|> planRule
|> Expect.equal (Ok (Database.Project [ 0 ] (Database.Read "greek")))
, test "a filtered read turns into a Select" <|
\_ ->
rule "mortal" [ "first name" ]
|> with "greek" [ var "first name", string "of Athens" ]
|> planRule
|> Expect.equal
(Database.Read "greek"
|> Database.Select (Database.Predicate 1 Database.Eq (Database.Constant (Database.String "of Athens")))
|> Database.Project [ 0 ]
|> Ok
)
, test "sharing a variable between two atoms results in a join" <|
\_ ->
rule "reachable" [ "a", "c" ]
|> with "link" [ var "a", var "b" ]
|> with "reachable" [ var "b", var "c" ]
|> planRule
|> Expect.equal
(Database.JoinOn
{ left = Database.Read "reachable"
, right = Database.Read "link"
, fields = [ ( 0, 1 ) ]
}
|> Database.Project [ 2, 1 ]
|> Ok
)
, test "filtering adds a predicate" <|
\_ ->
rule "adult" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter (gt "age" (int 20))
|> planRule
|> Expect.equal
(Database.Read "person"
|> Database.Select (Database.Predicate 1 Database.Gt (Database.Constant (Database.Int 20)))
|> Database.Project [ 0 ]
|> Ok
)
, test "filtering using negation adds a predicate" <|
\_ ->
rule "sibling" [ "a", "b" ]
|> with "parent" [ var "parent", var "a" ]
|> with "parent" [ var "parent", var "b" ]
|> filter (not_ (eq "a" (var "b")))
|> planRule
|> Expect.equal
(Database.JoinOn
{ left = Database.Read "parent"
, right = Database.Read "parent"
, fields = [ ( 0, 0 ) ]
}
|> Database.Select (Database.Not (Database.Predicate 3 Database.Eq (Database.Field 1)))
|> Database.Project [ 3, 1 ]
|> Ok
)
, test "filtering using or adds a predicate" <|
\_ ->
rule "teen" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter
(or
(gt "age" (int 12))
(lt "age" (int 20))
)
|> planRule
|> Expect.equal
(Database.Read "person"
|> Database.Select
(Database.Or
(Database.Predicate 1 Database.Gt (Database.Constant (Database.Int 12)))
(Database.Predicate 1 Database.Lt (Database.Constant (Database.Int 20)))
)
|> Database.Project [ 0 ]
|> Ok
)
, test "filtering using separate filters adds two filters" <|
\_ ->
rule "oldHockeyTeam" [ "name" ]
|> with "team" [ var "name", var "league", var "age" ]
|> filter (eq "league" (string "NHL"))
|> filter (gt "age" (int 50))
|> planRule
|> Expect.equal
(Database.Read "team"
|> Database.Select (Database.Predicate 2 Database.Gt (Database.Constant (Database.Int 50)))
|> Database.Select (Database.Predicate 1 Database.Eq (Database.Constant (Database.String "NHL")))
|> Database.Project [ 0 ]
|> Ok
)
, test "negation adds an outer join" <|
\_ ->
rule "unreachable" [ "a", "b" ]
|> with "node" [ var "a" ]
|> with "node" [ var "b" ]
|> without "reachable" [ var "a", var "b" ]
|> planRule
|> Expect.equal
(Database.OuterJoinOn
{ keep =
Database.JoinOn
{ fields = []
, left = Database.Read "node"
, right = Database.Read "node"
}
, drop = Database.Read "reachable"
, fields = [ ( 1, 0 ), ( 0, 1 ) ]
}
|> Database.Project [ 1, 0 ]
|> Ok
)
, describe "safety"
[ test "rules are required to have bodies" <|
\_ ->
rule "noBody" [ "a" ]
|> planRule
|> Expect.equal (Err NeedAtLeastOnePositiveAtom)
, test "rules are required to have at least one name" <|
\_ ->
rule "noNames" []
|> with "what" [ var "a" ]
|> planRule
|> Expect.equal (Err NeedAtLeastOneName)
, test "all terms in the head must appear in the body" <|
\_ ->
rule "bad" [ "a", "b" ]
|> with "fine" [ var "a" ]
|> planRule
|> Expect.equal (Err (VariableDoesNotAppearInBody "b"))
, test "you can't have just filters" <|
\_ ->
rule "bad" [ "a" ]
|> filter (eq "a" (string "no"))
|> planRule
|> Expect.equal (Err NeedAtLeastOnePositiveAtom)
, test "you can't filter on an unbound name" <|
\_ ->
rule "bad" [ "a" ]
|> with "fine" [ var "a" ]
|> filter (eq "b" (string "no"))
|> planRule
|> Expect.equal (Err (VariableDoesNotAppearInBody "b"))
, test "every name appearing in a negative atom must also appear in a positive atom" <|
\_ ->
rule "bad" [ "a" ]
|> with "an atom to avoid the must-have-one-positive-atom rule" [ var "b" ]
|> without "here's the problem" [ var "a" ]
|> planRule
|> Expect.equal (Err (VariableMustAppearInPositiveAtom "a"))
]
]
, describe "working with the database"
[ describe "insertion"
[ test "I can insert data" <|
\_ ->
empty
|> insert "greek" [ string "Socrates" ]
|> Expect.ok
, test "I can't insert variables" <|
\_ ->
empty
|> insert "greek" [ var "who?" ]
|> Expect.equal (Err (CannotInsertVariable "who?"))
]
, describe "reading"
[ test "I can read back a string I wrote" <|
\_ ->
empty
|> insert "greek" [ string "Socrates" ]
|> Result.andThen (into identity |> stringField 0 |> read "greek")
|> Expect.equal (Ok [ "Socrates" ])
, test "I can read back a number I wrote" <|
\_ ->
empty
|> insert "numbers" [ int 1 ]
|> Result.andThen (into identity |> intField 0 |> read "numbers")
|> Expect.equal (Ok [ 1 ])
, test "I get an error if I try to read the wrong type" <|
\_ ->
empty
|> insert "numbers" [ int 1 ]
|> Result.andThen (into identity |> stringField 0 |> read "numbers")
|> Expect.equal (Err (DecodingProblem (UnexpectedFieldType Database.IntType)))
, test "I get an error if I try to read an index that doesn't exist" <|
\_ ->
empty
|> insert "numbers" [ int 1 ]
|> Result.andThen (into identity |> stringField 1 |> read "numbers")
|> Expect.equal (Err (DecodingProblem (FieldNotFound 1)))
, test "I get an error if I try and read a table that doesn't exist" <|
\_ ->
empty
|> (into identity |> stringField 1 |> read "person")
|> Expect.equal (Err (DatabaseProblem (Database.RelationNotFound "person")))
, test "I get an empty list if I try and read a table which I pre-registered" <|
\_ ->
empty
|> register "person" [ Database.StringType ]
|> Result.andThen (into identity |> stringField 1 |> read "person")
|> Expect.equal (Ok [])
, test "I can read exactly one row" <|
\_ ->
empty
|> insert "person" [ string "Fred Rogers" ]
|> Result.andThen (into identity |> stringField 0 |> readOne "person")
|> Expect.equal (Ok "Fred Rogers")
, test "if I expect one row and there are 0, it's an error" <|
\_ ->
empty
|> register "person" [ Database.StringType ]
|> Result.andThen (into identity |> stringField 0 |> readOne "person")
|> Expect.equal (Err (ExpectedExactlyOneRow 0))
, test "if I expect one row and there si more than one, it's an error" <|
\_ ->
empty
|> insert "person" [ string "Fred Rogers" ]
|> Result.andThen (insert "person" [ string "Frida Kahlo" ])
|> Result.andThen (into identity |> stringField 0 |> readOne "person")
|> Expect.equal (Err (ExpectedExactlyOneRow 2))
]
, describe "deriving new tables"
[ test "I can derive a new table by renaming an existing table" <|
\_ ->
empty
|> insert "greek" [ string "Socrates" ]
|> Result.andThen
(derive
[ rule "mortal" [ "name" ]
|> with "greek" [ var "name" ]
]
)
|> Result.andThen
(into identity
|> stringField 0
|> read "mortal"
)
|> Expect.equal (Ok [ "Socrates" ])
, test "I can derive a new table by combining tables" <|
\_ ->
empty
|> insert "greek" [ string "Socrates" ]
|> Result.andThen (insert "french" [ string "Simone de Beauvoir" ])
|> Result.andThen
(derive
[ rule "mortal" [ "name" ]
|> with "greek" [ var "name" ]
, rule "mortal" [ "name" ]
|> with "french" [ var "name" ]
]
)
|> Result.andThen
(into identity
|> stringField 0
|> read "mortal"
)
|> Expect.equal (Ok [ "Simone de Beauvoir", "Socrates" ])
, test "I can filter tables by matching inside a `with` clause" <|
\_ ->
empty
|> insert "person" [ string "Simone de Beauvoir", string "philosopher" ]
|> Result.andThen (insert "person" [ string "Barbara Liskov", string "computer scientist" ])
|> Result.andThen
(derive
[ rule "philosopher" [ "name" ]
|> with "person" [ var "name", string "philosopher" ]
]
)
|> Result.andThen
(into identity
|> stringField 0
|> read "philosopher"
)
|> Expect.equal (Ok [ "Simone de Beauvoir" ])
, test "I can derive with recursion" <|
\_ ->
empty
|> insert "link" [ int 1, int 2 ]
|> Result.andThen (insert "link" [ int 2, int 3 ])
|> Result.andThen (insert "link" [ int 3, int 3 ])
|> Result.andThen (insert "link" [ int 3, int 4 ])
|> Result.andThen
(derive
[ rule "reachable" [ "a", "b" ]
|> with "link" [ var "a", var "b" ]
, rule "reachable" [ "a", "c" ]
|> with "link" [ var "a", var "b" ]
|> with "reachable" [ var "b", var "c" ]
]
)
|> Result.andThen
(into Tuple.pair
|> intField 0
|> intField 1
|> read "reachable"
)
|> Expect.equal
(Ok
[ ( 1, 2 )
, ( 1, 3 )
, ( 1, 4 )
, ( 2, 3 )
, ( 2, 4 )
, ( 3, 3 )
, ( 3, 4 )
]
)
, test "I can derive a new table via join" <|
\_ ->
empty
|> insert "mascot" [ string "Fredbird", string "Cardinals" ]
|> Result.andThen (insert "mascot" [ string "Louie", string "Blues" ])
|> Result.andThen (insert "mascot" [ string "Gritty", string "Flyers" ])
|> Result.andThen (insert "team" [ string "Cardinals", string "St. Louis" ])
|> Result.andThen (insert "team" [ string "Blues", string "St. Louis" ])
|> Result.andThen (insert "team" [ string "Flyers", string "Philadelphia" ])
|> Result.andThen
(derive
[ rule "hometown" [ "name", "hometown" ]
|> with "mascot" [ var "name", var "team" ]
|> with "team" [ var "team", var "hometown" ]
]
)
|> Result.andThen
(into Tuple.pair
|> stringField 0
|> stringField 1
|> read "hometown"
)
|> Expect.equal
(Ok
[ ( "Fredbird", "St. Louis" )
, ( "Gritty", "Philadelphia" )
, ( "Louie", "St. Louis" )
]
)
, test "I can derive a table using negation" <|
\_ ->
empty
|> insert "link" [ int 1, int 2 ]
|> Result.andThen (insert "link" [ int 2, int 3 ])
|> Result.andThen (insert "link" [ int 3, int 3 ])
|> Result.andThen (insert "link" [ int 3, int 4 ])
|> Result.andThen
(derive
[ rule "reachable" [ "a", "b" ]
|> with "link" [ var "a", var "b" ]
, rule "reachable" [ "a", "c" ]
|> with "link" [ var "a", var "b" ]
|> with "reachable" [ var "b", var "c" ]
, rule "node" [ "a" ]
|> with "link" [ var "a", var "b" ]
, rule "node" [ "b" ]
|> with "link" [ var "a", var "b" ]
, rule "unreachable" [ "a", "b" ]
|> with "node" [ var "a" ]
|> with "node" [ var "b" ]
|> without "reachable" [ var "a", var "b" ]
]
)
|> Result.andThen
(into Tuple.pair
|> intField 0
|> intField 1
|> read "unreachable"
)
|> Expect.equal
(Ok
[ ( 1, 1 )
, ( 2, 1 )
, ( 2, 2 )
, ( 3, 1 )
, ( 3, 2 )
, ( 4, 1 )
, ( 4, 2 )
, ( 4, 3 )
, ( 4, 4 )
]
)
, test "I cannot derive a program with negative recursion" <|
\_ ->
empty
|> insert "x" [ string "this doesn't matter, we just need it to trigger the rule under test" ]
|> Result.andThen
(derive
[ rule "p" [ "x" ]
|> with "x" [ var "x" ]
|> without "q" [ var "x" ]
, rule "q" [ "x" ]
|> with "x" [ var "x" ]
|> without "p" [ var "x" ]
]
)
|> Expect.equal (Err CannotHaveNegationInRecursiveQuery)
]
]
, describe "parsing rules from a string"
[ describe "valid programs"
[ test "a non-recursive rule" <|
\_ ->
expectParses
"mortal(thing) :- greek(thing)."
[ rule "mortal" [ "thing" ]
|> with "greek" [ var "thing" ]
]
, test "recursive rule" <|
\_ ->
expectParses
"""
reachable(a, b) :- link(a, b).
reachable(a, c) :- link(a, b), reachable(b, c).
"""
[ rule "reachable" [ "a", "b" ]
|> with "link" [ var "a", var "b" ]
, rule "reachable" [ "a", "c" ]
|> with "link" [ var "a", var "b" ]
|> with "reachable" [ var "b", var "c" ]
]
, test "an atom with an string" <|
\_ ->
expectParses
"""baseballTeam(name) :- team(name, "MLB")."""
[ rule "baseballTeam" [ "name" ]
|> with "team" [ var "name", string "MLB" ]
]
, test "an atom with an integer" <|
\_ ->
expectParses
"""
luckyBuilding(name) :- building(name, 7).
"""
[ rule "luckyBuilding" [ "name" ]
|> with "building" [ var "name", int 7 ]
]
, describe "filters"
[ test "less than" <|
\_ ->
expectParses
"""
child(name) :-
person(name, age),
age < 18.
"""
[ rule "child" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter (lt "age" (int 18))
]
, test "greater than" <|
\_ ->
expectParses
"""
adult(name) :-
person(name, age),
age > 17.
"""
[ rule "adult" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter (gt "age" (int 17))
]
, test "equal to" <|
\_ ->
expectParses
"""
spider(name) :-
thing(name, legs),
legs = 8.
"""
[ rule "spider" [ "name" ]
|> with "thing" [ var "name", var "legs" ]
|> filter (eq "legs" (int 8))
]
, test "or" <|
\_ ->
expectParses
"""
adult(name) :-
person(name, age),
age = 18 || age > 18.
"""
[ rule "adult" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter
(or
(eq "age" (int 18))
(gt "age" (int 18))
)
]
, test "greater than or equal to" <|
\_ ->
expectParses
"""
adult(name) :-
person(name, age),
age >= 18.
"""
[ rule "adult" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter
(or
(gt "age" (int 18))
(eq "age" (int 18))
)
]
, test "less than or equal to" <|
\_ ->
expectParses
"""
child(name) :-
person(name, age),
age <= 17.
"""
[ rule "child" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter
(or
(lt "age" (int 17))
(eq "age" (int 17))
)
]
]
, test "rule with negation" <|
\_ ->
expectParses
"""
unreachable(a, b) :-
node(a),
node(b),
not reachable(a, b).
"""
[ rule "unreachable" [ "a", "b" ]
|> with "node" [ var "a" ]
|> with "node" [ var "b" ]
|> without "reachable" [ var "a", var "b" ]
]
]
, describe "errors"
[ todo "errors!" ]
]
]
expectParses : String -> List Rule -> Expectation
expectParses input expectedOutput =
case Datalog.parse input of
Ok actualOutput ->
Expect.equal expectedOutput actualOutput
Err err ->
case err of
ParsingProblem deadEnds ->
-- this should eventually just show the nicest version possible
-- of the error message. Probably using whatever `errorToString`
-- thing we end up with.
let
rows : List String
rows =
String.lines input
sourceRow : Int -> Maybe String
sourceRow i =
rows
|> List.drop (i - 1)
|> List.head
pointer : Int -> String
pointer i =
String.repeat (i - 1) " " ++ "^"
in
deadEnds
|> List.map
(\{ row, col, contextStack, problem } ->
String.join "\n"
[ "There's a problem at row " ++ String.fromInt row ++ ", column " ++ String.fromInt col ++ ":"
, ""
, Maybe.withDefault "???" (sourceRow row)
, pointer col
, ""
, "With this problem:"
, Debug.toString problem
, ""
, "With this context:"
, contextStack
|> List.map (\context -> " - " ++ Debug.toString context)
|> String.join "\n"
]
)
|> String.join "\n\n==========================================================\n\n"
|> (++) "I ran into a problem parsing. I got these dead ends:\n\n"
|> Expect.fail
_ ->
-- fall back on a reasonable implementation
Expect.equal (Ok expectedOutput) (Err err)