Skip to content

Commit

Permalink
solve according to strata instead of naively
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Dec 16, 2020
1 parent eb243c2 commit 6ad133a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/Datalog.elm
Expand Up @@ -160,21 +160,21 @@ insertAtom atom database =


solve : Program -> Database
solve program_ =
solveHelp program_ Dict.empty
solve (Program rules) =
List.foldl solveHelp Dict.empty rules


solveHelp : Program -> Database -> Database
solveHelp ((Program rules) as program_) database =
solveHelp : List Rule -> Database -> Database
solveHelp rules database =
let
expanded =
List.foldl evaluateRule database (List.concat rules)
List.foldl evaluateRule database rules
in
if expanded == database then
database

else
solveHelp program_ expanded
solveHelp rules expanded


evaluateRule : Rule -> Database -> Database
Expand Down
50 changes: 33 additions & 17 deletions tests/DatalogTests.elm
Expand Up @@ -122,11 +122,9 @@ solveTest =
[ atom "siblings" [ string "Child B", string "Child A" ]
, atom "siblings" [ string "Child A", string "Child B" ]
]
]
, only <|
describe "(temporary) stratification"
[ test "stratifies a stratifiable program" <|
\_ ->
, test "stratifies a stratifiable program" <|
\_ ->
unsafeProgram
[ Rule.fact (atom "link" [ string "a", string "b" ])
, Rule.fact (atom "link" [ string "b", string "c" ])
, Rule.fact (atom "link" [ string "c", string "c" ])
Expand All @@ -153,18 +151,36 @@ solveTest =
, negative (atom "reachable" [ variable "a", variable "b" ])
]
]
|> unsafeProgram
|> Ok
|> Expect.err
, test "does not stratify an unstratifiable program" <|
\_ ->
[ Rule.rule (atom "p" [ variable "x" ]) [ negative (atom "q" [ variable "x" ]) ]
, Rule.rule (atom "q" [ variable "x" ]) [ negative (atom "p" [ variable "x" ]) ]
]
|> unsafeProgram
|> Ok
|> Expect.err
]
|> solve
|> get ( "unreachable", 2 )
|> Expect.equal
[ atom "unreachable" [ string "d", string "d" ]
, atom "unreachable" [ string "d", string "c" ]
, atom "unreachable" [ string "d", string "b" ]
, atom "unreachable" [ string "d", string "a" ]
, atom "unreachable" [ string "c", string "b" ]
, atom "unreachable" [ string "c", string "a" ]
, atom "unreachable" [ string "b", string "b" ]
, atom "unreachable" [ string "b", string "a" ]
, atom "unreachable" [ string "a", string "a" ]
]
, test "does not stratify an unstratifiable program" <|
\_ ->
[ Rule.rule (atom "p" [ variable "x" ]) [ negative (atom "q" [ variable "x" ]) ]
, Rule.rule (atom "q" [ variable "x" ]) [ negative (atom "p" [ variable "x" ]) ]
]
|> List.map
(\ruleOrErr ->
case ruleOrErr of
Ok rule ->
rule

Err err ->
Debug.todo (Debug.toString err)
)
|> program
|> Expect.equal (Err Datalog.CycleWithNegation)
]
]


Expand Down

0 comments on commit 6ad133a

Please sign in to comment.