Compare commits

..

1 Commits

Author SHA1 Message Date
Patrick Aldis
d28638e286 Current progress 2026-03-03 11:05:11 +00:00
2 changed files with 58 additions and 68 deletions

View File

@ -1,5 +1,4 @@
{-# LANGUAGE MultilineStrings #-} {-# LANGUAGE MultilineStrings #-}
{-# LANGUAGE BlockArguments #-}
module Datalog.Parser ( module Datalog.Parser (
parseTerm, parseTerm,
@ -50,53 +49,32 @@ parseVar = Var () . VarId . T.pack <$> lexeme (liftA2 (:) upperChar (many alphaN
parseTerm :: Parser Term parseTerm :: Parser Term
parseTerm = parseVar <|> parseCon parseTerm = parseVar <|> parseCon
parseAtom :: Parser Atom parseAtom :: Parser (Atom' SrcLoc)
parseAtom = do parseAtom = annotateSrcLoc $ do
rel <- RelId . T.pack <$> lexeme (liftA2 (:) lowerChar (many alphaNumChar)) rel <- RelId . T.pack <$> lexeme (liftA2 (:) lowerChar (many alphaNumChar))
args <- parens (parseTerm `sepBy` comma) args <- parens (parseTerm `sepBy` comma)
return (Atom () rel args) return (Atom () rel args)
parseQuery :: Parser [Atom] parseQuery :: Parser [Atom' SrcLoc]
parseQuery = parseAtom `sepBy` comma parseQuery = parseAtom `sepBy` comma
parseFact :: Parser Rule parseFact :: Parser (Rule' SrcLoc)
parseFact = do parseFact = annotateSrcLoc $ do
headAtom <- parseAtom headAtom <- parseAtom
period period
return (Rule () headAtom []) return (Rule NoLoc headAtom [])
parseRule :: Parser Rule parseRule :: Parser (Rule' SrcLoc)
parseRule = parseRule =
annotateSrcLoc $
try parseFact <|> do try parseFact <|> do
headAtom <- parseAtom <* symbol ":-" headAtom <- parseAtom <* symbol ":-"
bodyAtoms <- parseQuery bodyAtoms <- parseQuery
period period
return (Rule () headAtom bodyAtoms) return (Rule NoLoc headAtom bodyAtoms)
parseRule' :: Parser (Rule' SrcLoc)
parseRule' = _
-- parseProgram :: Parser Program
parseProgram :: Parser (Program' SrcLoc) parseProgram :: Parser (Program' SrcLoc)
parseProgram = do parseProgram = annotateSrcLoc $ Program NoLoc <$> many parseRule
-- annotateSrcLoc $
s <- getSourcePos
c <- many parseRule'
e <- getSourcePos
-- Program _ <$> many parseRule
pure $ Program (SrcLoc s e) c
parseProgram' :: Parser (Program' SrcLoc)
parseProgram' = parseThingWithSub (many parseRule') Program
parseThingWithSub :: (Parser c) -> (SrcLoc -> c -> f SrcLoc) -> Parser (f SrcLoc)
parseThingWithSub parseSub f = do
-- annotateSrcLoc $
s <- getSourcePos
-- c <- many parseRule'
c <- parseSub
e <- getSourcePos
-- Program _ <$> many parseRule
pure $ f (SrcLoc s e) c
annotateSrcLoc :: (Functor f) => Parser (f a) -> Parser (f SrcLoc) annotateSrcLoc :: (Functor f) => Parser (f a) -> Parser (f SrcLoc)
annotateSrcLoc p = do annotateSrcLoc p = do
@ -105,10 +83,12 @@ annotateSrcLoc p = do
f <- getSourcePos f <- getSourcePos
pure (SrcLoc s f <$ res) pure (SrcLoc s f <$ res)
data SrcLoc = SrcLoc data SrcLoc
= SrcLoc
{ start :: SourcePos { start :: SourcePos
, end :: SourcePos , end :: SourcePos
} }
| NoLoc
deriving (Show) deriving (Show)
test = do test = do

View File

@ -3,10 +3,10 @@
module Datalog.Syntax where module Datalog.Syntax where
import Data.Char (isUpper) import Data.Char (isUpper)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text) import Data.Text (Text)
import Data.Text qualified as T import Data.Text qualified as T
import Data.Set qualified as Set
import Data.Set (Set)
newtype ConId = ConId Text newtype ConId = ConId Text
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
@ -48,6 +48,16 @@ type Program = Program' ()
data Program' a = Program a [Rule' a] data Program' a = Program a [Rule' a]
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
class Decorable t where
decorateNode :: a -> t a -> t a
instance (Decorable Program') where decorateNode x (Program _ rs) = Program x rs
instance (Decorable Rule') where decorateNode x (Rule _ a as) = Rule x a as
instance (Decorable Atom') where decorateNode x (Atom _ relId ts) = Atom x relId ts
instance (Decorable Term') where
decorateNode x (Var _ varId) = Var x varId
decorateNode x (Con _ conId) = Con x conId
class HasConstants a where class HasConstants a where
constants :: a -> Set ConId constants :: a -> Set ConId
@ -56,7 +66,7 @@ instance HasConstants (Term' a) where
Con _ x -> Set.singleton x Con _ x -> Set.singleton x
Var _ _ -> Set.empty Var _ _ -> Set.empty
instance (HasConstants a) => HasConstants [a] where instance HasConstants a => HasConstants [a] where
constants xs = Set.unions (map constants xs) constants xs = Set.unions (map constants xs)
instance HasConstants (Atom' a) where instance HasConstants (Atom' a) where