Foods.pgf
readPGF :: FilePath -> IO PGF linearize :: PGF -> Language -> Tree -> String parse :: PGF -> Language -> Category -> String -> [Tree] linearizeAll :: PGF -> Tree -> [String] linearizeAllLang :: PGF -> Tree -> [(Language,String)] parseAll :: PGF -> Category -> String -> [[Tree]] parseAllLang :: PGF -> Category -> String -> [(Language,[Tree])] languages :: PGF -> [Language] categories :: PGF -> [Category] startCat :: PGF -> Category
import PGF main = do pgf <- readPGF "Foods.pgf" interact (unlines . map (translate pgf) . lines) translate pgf s = case parseAllLang pgf (startCat pgf) s of (from,tree:_):_ -> unlines [linearize pgf to tree | to <- languages pgf, to/=from] _ -> "NO PARSE"
Is 123 prime ? No. 77 est impair ? Oui.
abstract Arithmetic = { flags startcat=Question ; cat Answer; Object; Question; fun Even, Odd, Prime : Object -> Question; Number : Int -> Object; Yes, No : Answer; }
concrete ArithmeticEng of Arithmetic = { lincat Answer, Object, Question = Str; lin Even object = is "even" object; Odd object = is "odd" object; Prime object = is "prime" object; Number int = int.s; No = "No."; Yes = "Yes."; oper is : Str -> Str -> Str = \ pred,obj -> "is"++obj++pred++"?"; }
import PGF 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 = ...
module Arithmetic where data GAnswer = GNo | GYes data GObject = GNumber GInt data GQuestion = GEven GObject | GOdd GObject | GPrime GObject class Gf a where gf :: a -> Tree fg :: Tree -> a
import PGF import Arithmetic 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]]
Answer: ...
answer.cgi
:
gf -make --output-format=js FoodEng.gf FoodIta.gf