math with gfcc

This commit is contained in:
aarne
2007-09-20 13:31:24 +00:00
parent c7f59d0bad
commit 549447458c
3 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
all: gf hs run
gf:
echo "pm -printer=gfcc | wf math.gfcc" | gf MathEng.gf MathFre.gf
hs: gf
echo "pg -printer=gfcc_haskell | wf GSyntax.hs" | gf MathEng.gf MathFre.gf
run: hs
ghc --make -o ./mathc TransferLoopGFCC.hs
strip mathc
clean:
rm -f *.gfc *.gfr *.o *.hi
distclean:
rm -f GSyntax.hs math math.gfcc *.gfc *.gfr *.o *.hi

View File

@@ -0,0 +1,26 @@
module TransferDefGFCC where
import GF.Canon.GFCC.GFCCAPI (Tree)
import GSyntax
transfer :: Tree -> Tree
transfer = gf . answer . fg
answer :: GQuestion -> GAnswer
answer p = case p of
GOdd x -> test odd x
GEven x -> test even x
GPrime x -> test prime x
value :: GObject -> Int
value e = case e of
GNumber (GInt i) -> fromInteger i
test :: (Int -> Bool) -> GObject -> GAnswer
test f x = if f (value x) then GYes else GNo
prime :: Int -> Bool
prime x = elem x primes where
primes = sieve [2 .. x]
sieve (p:xs) = p : sieve [ n | n <- xs, n `mod` p > 0 ]
sieve [] = []

View File

@@ -0,0 +1,23 @@
module Main where
import GF.Canon.GFCC.GFCCAPI
import TransferDefGFCC (transfer)
main :: IO ()
main = do
gr <- file2grammar "math.gfcc"
loop (translate transfer gr)
loop :: (String -> String) -> IO ()
loop trans = do
s <- getLine
if s == "quit" then putStrLn "bye" else do
putStrLn $ trans s
loop trans
translate :: (Tree -> Tree) -> MultiGrammar -> String -> String
translate tr gr = unlines . map transLine . lines where
transLine s = case parseAllLang gr "Question" s of
(lg,t:_):_ -> linearize gr lg (tr t)
_ -> "NO PARSE"