62 lines
1.7 KiB
Haskell
Raw Normal View History

2026-01-21 11:24:30 +00:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Redundant flip" #-}
2026-01-21 17:11:13 +00:00
{-# LANGUAGE ImportQualifiedPost #-}
2026-01-21 11:24:30 +00:00
module Datalog.NaiveDatabase where
import Data.Map (Map)
import Data.Set (Set)
2026-01-21 17:11:13 +00:00
import Data.Set qualified as Set
import Data.Text (Text)
import qualified Data.Text as T
import Datalog.DatalogParser
import qualified Data.Map as Map
import Text.Megaparsec (ParseErrorBundle)
import Data.Void
import Control.Exception (Exception)
import Control.Exception.Base
2026-01-21 11:24:30 +00:00
data Value =
ValueInt Int |
ValueSymbol String
data NaiveDatabase = NaiveDatabase {
2026-01-21 17:11:13 +00:00
relations :: Map Text Int,
2026-01-21 11:24:30 +00:00
values :: Set Value
2026-01-21 17:11:13 +00:00
}
emptyDB :: NaiveDatabase
emptyDB = NaiveDatabase {
relations = Map.empty,
values = Set.empty
}
withFacts :: [Text] -> NaiveDatabase
withFacts facts =
foldl addFact emptyDB (extractFact <$> facts) where
extractFact:: Text -> Literal
extractFact factText =
case (parseDatalog factText) of
Right (Fact fact) -> fact
Right otherStatement -> throw $ NonFactException factText otherStatement
Left ex -> throw $ CannotParseStatementException factText ex
addFact :: NaiveDatabase -> Literal -> NaiveDatabase
addFact db lit =
db
query :: NaiveDatabase -> Text -> Text
query db qText =
case (parseDatalog qText) of
Right (Query texts literals) -> "#NYI"
Right otherStatement -> throw $ NonQueryException qText otherStatement
Left ex -> throw $ CannotParseStatementException qText ex
data NaiveDatabaseException
= CannotParseStatementException Text (ParseErrorBundle Text Void) |
NonFactException Text Statement |
NonQueryException Text Statement
deriving (Show)
instance Exception NaiveDatabaseException