diff --git a/src/Database/Datalog.elm b/src/Database/Datalog.elm index e0684db..129609e 100644 --- a/src/Database/Datalog.elm +++ b/src/Database/Datalog.elm @@ -35,6 +35,7 @@ empty = type Problem = NeedAtLeastOnePositiveAtom + | NeedAtLeastOneName | VariableDoesNotAppearInBody String | VariableMustAppearInPositiveAtom String | CannotInsertVariable String @@ -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 diff --git a/tests/Database/DatalogTests.elm b/tests/Database/DatalogTests.elm index 44dd18e..91cc693 100644 --- a/tests/Database/DatalogTests.elm +++ b/tests/Database/DatalogTests.elm @@ -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" ]