Answer.hs

Plain text version of Answer.hs

import PGF
import Arithmetic

main :: IO ()
main = do pgf <- readPGF "Arithmetic.pgf"
          interact (unlines . map (translate pgf transfer) . lines)

translate :: PGF -> (Tree->Tree) -> String -> String
translate pgf transfer s =
  case parseAllLang pgf (startCat pgf) s of
     (lang,tree:_):_ -> linearize pgf lang (transfer tree)
     _ -> "NO PARSE"

transfer :: Tree -> Tree
transfer = gf . answer . fg

answer :: GQuestion -> GAnswer
answer (GEven  x) = test even  x
answer (GOdd   x) = test odd   x
answer (GPrime x) = test prime x

test :: (Int->Bool) -> GObject -> GAnswer
test p (GNumber (GInt x)) = if p x then GYes else GNo

prime n = and [n `mod` d /= 0 | d <-[2..n-1]] -- could be more efficient...