module Language.Prolog.NanoProlog.Interpreter where
import Language.Prolog.NanoProlog.NanoProlog
import Text.ParserCombinators.UU
import System.IO
run :: IO ()
run = do hSetBuffering stdin LineBuffering
putStrLn "File with rules?"
fn <- getLine
s <- readFile fn
let (rules, errors) = startParse (pList pRule) s
if null errors then do mapM_ print rules
loop rules
else do putStrLn "No rules parsed"
mapM_ print errors
run
loop :: [Rule] -> IO ()
loop rules = do putStrLn "goal? "
s <- getLine
unless (s == "quit") $
do let (goal, errors) = startParse pFun s
if null errors
then printSolutions (solve rules emptyEnv [("0",goal)])
else do putStrLn "Some goals were expected:"
mapM_ print errors
loop rules
printSolutions :: Result -> IO ()
printSolutions result = sequence_
[ do sequence_ [ putStrLn (prefix ++ " " ++ show (subst env pr))
| (prefix, pr@(p :<-: pp)) <- reverse proof
]
putStr "substitution: "
putStrLn (show' env)
void getLine
| (proof, env) <- enumerateDepthFirst [] result ]