Skip to content

Commit

Permalink
require at least one name
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 12, 2021
1 parent e211d16 commit a83aeb7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/Database/Datalog.elm
Expand Up @@ -35,6 +35,7 @@ empty =

type Problem
= NeedAtLeastOnePositiveAtom
| NeedAtLeastOneName
| VariableDoesNotAppearInBody String
| VariableMustAppearInPositiveAtom String
| CannotInsertVariable String
Expand Down Expand Up @@ -453,27 +454,31 @@ planRule (Rule (Atom _ headTerms) bodyAtoms) =
in
Result.andThen
(\( names, plan ) ->
headTerms
|> foldrResult
(\term soFar ->
case term of
Variable name ->
case indexOf name names of
Just idx ->
Ok (idx :: soFar)

Nothing ->
Err (VariableDoesNotAppearInBody name)

Constant _ ->
-- It's fine to just ignore this, since
-- we disallow rules having constants by
-- construction. This will be an unfortunate
-- bug if we ever change that, though! :\
Ok soFar
)
[]
|> Result.map (\indexes -> Database.Project indexes plan)
if List.isEmpty headTerms then
Err NeedAtLeastOneName

else
headTerms
|> foldrResult
(\term soFar ->
case term of
Variable name ->
case indexOf name names of
Just idx ->
Ok (idx :: soFar)

Nothing ->
Err (VariableDoesNotAppearInBody name)

Constant _ ->
-- It's fine to just ignore this, since
-- we disallow rules having constants by
-- construction. This will be an unfortunate
-- bug if we ever change that, though! :\
Ok soFar
)
[]
|> Result.map (\indexes -> Database.Project indexes plan)
)
planned

Expand Down
6 changes: 6 additions & 0 deletions tests/Database/DatalogTests.elm
Expand Up @@ -133,6 +133,12 @@ datalogTests =
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" ]
Expand Down

0 comments on commit a83aeb7

Please sign in to comment.