diff --git a/haskell-experiments/src/Datalog/NaiveDatabase.hs b/haskell-experiments/src/Datalog/NaiveDatabase.hs index a8c9ef9..df45e6d 100644 --- a/haskell-experiments/src/Datalog/NaiveDatabase.hs +++ b/haskell-experiments/src/Datalog/NaiveDatabase.hs @@ -24,24 +24,28 @@ data Value = data NaiveDatabase = NaiveDatabase { relations :: Map RelationId Relation, - constants :: Set ConstantId + constants :: Set Constant } data Relation = Relation { arity :: Int, - tuples :: Set [ConstantId] + tuples :: Set [Constant] } -newtype RelationId = RelationId Text - deriving (Eq, Ord, Show) +-- newtype RelationId = RelationId Text +-- deriving (Eq, Ord, Show) -newtype ConstantId = ConstantId Text - deriving (Eq, Ord, Show) +-- Our constants will be the terms of the Datalog grammar - ints/variables/symbols +type Constant = Term +type RelationId = Text + +-- newtype Constant = Constant Text +-- deriving (Eq, Ord, Show) emptyDB :: NaiveDatabase emptyDB = NaiveDatabase { relations = Map.empty, - constants = Set.empty + constants = Set.empty -- the Herbrand universe } withFacts :: [Text] -> NaiveDatabase @@ -54,8 +58,13 @@ withFacts facts = Right otherStatement -> throw $ NonFactException factText otherStatement Left ex -> throw $ CannotParseStatementException factText ex addFact :: NaiveDatabase -> Literal -> NaiveDatabase - addFact db lit = - db + addFact (NaiveDatabase relations constants) (Literal neg relationName terms) = + NaiveDatabase newRelations newConstants where + newRelations = + case Map.lookup relationName relations of + Nothing -> relations + Just relation -> relations + newConstants = Set.union constants $ Set.fromList terms query :: NaiveDatabase -> Text -> Text query db qText =