From a775f753e0e1b0a0a04c42fa4446b93e02f9f7c9 Mon Sep 17 00:00:00 2001 From: Cale Gibbard Date: Mon, 26 Jan 2026 12:18:29 -0500 Subject: [PATCH] Fix a bug in projectTuple where it wasn't ensuring that variables that appeared multiple times were bound to the same thing in the resulting set of tuples. --- src/Datalog/Eval.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Datalog/Eval.hs b/src/Datalog/Eval.hs index 9b26770..28703c0 100644 --- a/src/Datalog/Eval.hs +++ b/src/Datalog/Eval.hs @@ -145,10 +145,16 @@ evalConjunction db atoms = do return (foldr (><) (Set.singleton Map.empty) rs) projectTuple :: Set ConId -> Map VarId ConId -> [Term] -> Set [ConId] -projectTuple univ m = Set.fromList . mapM proj +projectTuple univ m0 ts0 = Set.fromList $ proj m0 ts0 where - proj (Con c) = [c] - proj (Var v) = Map.findWithDefault (Set.toList univ) v (fmap (:[]) m) + proj _ [] = [[]] + proj m (Con c : ts) = map (c:) (proj m ts) + proj m (Var v : ts) = case Map.lookup v m of + Just c -> map (c:) (proj m ts) + Nothing -> do + c <- Set.toList univ + s <- proj (Map.insert v c m) ts + return (c : s) immediateConsequences :: Database -> Rule -> Either EvalError (Set [ConId]) immediateConsequences db (Atom _ ts :- bodyAtoms) = do