Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ import qualified Data.List.NonEmpty as NE (fromList, head, last, tail, toList)
import Data.Maybe (mapMaybe)
import Syntax (
Assoc (AssocLeft, AssocRight),
Atom,
Goal,
Operator (..),
ProgramClause,
Term (..),
VariableName (VariableName),
arguments,
Expand All @@ -63,19 +62,19 @@ import Syntax (
var,
)

consult :: FilePath -> IO (Either ParseError [(Atom, [Term], [Goal])])
consult :: FilePath -> IO (Either ParseError [ProgramClause])
consult = fmap consultString . readFile

consultString :: String -> Either ParseError [(Atom, [Term], [Goal])]
consultString :: String -> Either ParseError [ProgramClause]
consultString = parse (whitespace >> program <* eof) "(input)"

parseQuery :: String -> Either ParseError [Term]
parseQuery = parse (whitespace >> terms <* eof) "(query)"

program :: ParsecT String () Identity [(Atom, [Term], [Goal])]
program :: ParsecT String () Identity [ProgramClause]
program = many (clause <* reservedOp "." <* whitespace)

clause :: ParsecT String () Identity (Atom, [Term], [Goal])
clause :: ParsecT String () Identity ProgramClause
clause = do
t <- struct <* whitespace
dcg t <|> normal t
Expand Down
2 changes: 2 additions & 0 deletions src/Prolog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Prolog (
prettifyVariableName,
prettifyTerm,
prettifyClause,
ProgramClause,
)
where

Expand Down Expand Up @@ -76,6 +77,7 @@ import Syntax (
Clause (..),
Goal,
Program,
ProgramClause,
Term (..),
VariableName (..),
cut,
Expand Down
8 changes: 4 additions & 4 deletions src/Quote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Text.Parsec (eof, parse)
import Text.Parsec.String (Parser)

import Parser (clause, program, term, terms, whitespace)
import Syntax (Atom, Clause (..), Goal, Term (..))
import Syntax (Clause (..), ProgramClause, Term (..))

t :: QuasiQuoter
t = prologQuasiQuoter term "term"
Expand Down Expand Up @@ -41,8 +41,8 @@ parsePrologExp parser name str = do
const (fail $ "Quasi-quoted expressions of type " ++ show (typeOf x) ++ " are not implemented.")
`extQ` unquote -- Term
`extQ` (listE . map unquote) -- [Term]
`extQ` unquoteClause -- (Atom, [Term], [Goal])
`extQ` (listE . map unquoteClause) -- [(Atom, [Term], [Goal])]
`extQ` unquoteClause -- ProgramClause
`extQ` (listE . map unquoteClause) -- [ProgramClause]
$ x
Left e -> fail (show e)
where
Expand All @@ -52,7 +52,7 @@ parsePrologExp parser name str = do
unquote (Struct a ts') = [e|Struct a $(listE $ map unquote ts')|]
unquote t' = lift t'

unquoteClause :: (Atom, [Term], [Goal]) -> Q Exp
unquoteClause :: ProgramClause -> Q Exp
unquoteClause (name', args, rhs) =
[e|Clause name' $(listE $ map unquote args) $(listE $ map unquote rhs)|]

Expand Down
4 changes: 4 additions & 0 deletions src/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Syntax (
prettifyVariableName,
prettifyTerm,
prettifyClause,
ProgramClause,
)
where

Expand Down Expand Up @@ -59,6 +60,9 @@ var = Var . VariableName 0
cut :: Term
cut = Cut 0

-- | An abbreviation for a clause defined in a program
type ProgramClause = (Atom, [Term], [Goal])

data Clause
= Clause {predicateName :: Atom, predicateArgs :: [Term], rhs_ :: [Goal]}
| ClauseFn {predicateName :: Atom, predicateArgs :: [Term], fn :: [Term] -> [Goal]}
Expand Down
Loading