adjusting types, developing addFact
This commit is contained in:
parent
52fad6e4a1
commit
36622caf8b
@ -24,24 +24,28 @@ data Value =
|
|||||||
|
|
||||||
data NaiveDatabase = NaiveDatabase {
|
data NaiveDatabase = NaiveDatabase {
|
||||||
relations :: Map RelationId Relation,
|
relations :: Map RelationId Relation,
|
||||||
constants :: Set ConstantId
|
constants :: Set Constant
|
||||||
}
|
}
|
||||||
|
|
||||||
data Relation = Relation {
|
data Relation = Relation {
|
||||||
arity :: Int,
|
arity :: Int,
|
||||||
tuples :: Set [ConstantId]
|
tuples :: Set [Constant]
|
||||||
}
|
}
|
||||||
|
|
||||||
newtype RelationId = RelationId Text
|
-- newtype RelationId = RelationId Text
|
||||||
deriving (Eq, Ord, Show)
|
-- deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
newtype ConstantId = ConstantId Text
|
-- Our constants will be the terms of the Datalog grammar - ints/variables/symbols
|
||||||
deriving (Eq, Ord, Show)
|
type Constant = Term
|
||||||
|
type RelationId = Text
|
||||||
|
|
||||||
|
-- newtype Constant = Constant Text
|
||||||
|
-- deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
emptyDB :: NaiveDatabase
|
emptyDB :: NaiveDatabase
|
||||||
emptyDB = NaiveDatabase {
|
emptyDB = NaiveDatabase {
|
||||||
relations = Map.empty,
|
relations = Map.empty,
|
||||||
constants = Set.empty
|
constants = Set.empty -- the Herbrand universe
|
||||||
}
|
}
|
||||||
|
|
||||||
withFacts :: [Text] -> NaiveDatabase
|
withFacts :: [Text] -> NaiveDatabase
|
||||||
@ -54,8 +58,13 @@ withFacts facts =
|
|||||||
Right otherStatement -> throw $ NonFactException factText otherStatement
|
Right otherStatement -> throw $ NonFactException factText otherStatement
|
||||||
Left ex -> throw $ CannotParseStatementException factText ex
|
Left ex -> throw $ CannotParseStatementException factText ex
|
||||||
addFact :: NaiveDatabase -> Literal -> NaiveDatabase
|
addFact :: NaiveDatabase -> Literal -> NaiveDatabase
|
||||||
addFact db lit =
|
addFact (NaiveDatabase relations constants) (Literal neg relationName terms) =
|
||||||
db
|
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 :: NaiveDatabase -> Text -> Text
|
||||||
query db qText =
|
query db qText =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user