forked from GitHub/gf-core
merged Gf and Fg classes in generated Haskell
This commit is contained in:
@@ -1,18 +1,11 @@
|
|||||||
all: gf hs run
|
all:
|
||||||
|
gfc --make -haskell MathEng.gf MathFre.gf
|
||||||
gf:
|
|
||||||
echo "pm | wf math.gfcm" | gf MathEng.gf MathFre.gf
|
|
||||||
|
|
||||||
hs: gf
|
|
||||||
echo "pg -printer=haskell | wf GSyntax.hs" | gf math.gfcm
|
|
||||||
|
|
||||||
run: hs
|
|
||||||
ghc --make -o ./math TransferLoop.hs
|
ghc --make -o ./math TransferLoop.hs
|
||||||
strip math
|
strip math
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.gfc *.gfr *.o *.hi
|
rm -f *.gfo *.o *.hi
|
||||||
|
|
||||||
distclean:
|
distclean:
|
||||||
rm -f GSyntax.hs math math.gfcm *.gfc *.gfr *.o *.hi
|
rm -f GSyntax.hs math Math.gfcc *.gfo *.o *.hi
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
module TransferDef where
|
module TransferDef where
|
||||||
|
|
||||||
import GF.Embed.EmbedAPI (Tree)
|
import GF.GFCC.API (Tree)
|
||||||
import GSyntax
|
import GSyntax
|
||||||
|
|
||||||
transfer :: Tree -> Tree
|
transfer :: Tree -> Tree
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
module TransferDefGFCC where
|
|
||||||
|
|
||||||
import GF.GFCC.API (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 [] = []
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import GF.Embed.EmbedAPI
|
import GF.GFCC.API
|
||||||
import TransferDef (transfer)
|
import TransferDef (transfer)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
gr <- file2grammar "math.gfcm"
|
gr <- file2grammar "Math.gfcc"
|
||||||
loop (translate transfer gr)
|
loop (translate transfer gr)
|
||||||
|
|
||||||
loop :: (String -> String) -> IO ()
|
loop :: (String -> String) -> IO ()
|
||||||
@@ -17,7 +17,7 @@ loop trans = do
|
|||||||
|
|
||||||
translate :: (Tree -> Tree) -> MultiGrammar -> String -> String
|
translate :: (Tree -> Tree) -> MultiGrammar -> String -> String
|
||||||
translate tr gr = unlines . map transLine . lines where
|
translate tr gr = unlines . map transLine . lines where
|
||||||
transLine s = case parseAllLang gr (startCat gr) s of
|
transLine s = case parseAllLang gr "Question" s of
|
||||||
(lg,t:_):_ -> linearize gr lg (tr t)
|
(lg,t:_):_ -> linearize gr lg (tr t)
|
||||||
_ -> "NO PARSE"
|
_ -> "NO PARSE"
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
module Main where
|
|
||||||
|
|
||||||
import GF.GFCC.API
|
|
||||||
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"
|
|
||||||
|
|
||||||
@@ -29,13 +29,13 @@ import qualified Data.Map as Map
|
|||||||
-- | the main function
|
-- | the main function
|
||||||
grammar2haskell :: GFCC -> String
|
grammar2haskell :: GFCC -> String
|
||||||
grammar2haskell gr = encodeUTF8 $ foldr (++++) [] $
|
grammar2haskell gr = encodeUTF8 $ foldr (++++) [] $
|
||||||
haskPreamble ++ [datatypes gr', gfinstances gr', fginstances gr']
|
haskPreamble ++ [datatypes gr', gfinstances gr']
|
||||||
where gr' = hSkeleton gr
|
where gr' = hSkeleton gr
|
||||||
|
|
||||||
grammar2haskellGADT :: GFCC -> String
|
grammar2haskellGADT :: GFCC -> String
|
||||||
grammar2haskellGADT gr = encodeUTF8 $ foldr (++++) [] $
|
grammar2haskellGADT gr = encodeUTF8 $ foldr (++++) [] $
|
||||||
["{-# OPTIONS_GHC -fglasgow-exts #-}"] ++
|
["{-# OPTIONS_GHC -fglasgow-exts #-}"] ++
|
||||||
haskPreamble ++ [datatypesGADT gr', gfinstances gr', fginstances gr']
|
haskPreamble ++ [datatypesGADT gr', gfinstances gr']
|
||||||
where gr' = hSkeleton gr
|
where gr' = hSkeleton gr
|
||||||
|
|
||||||
-- | by this you can prefix all identifiers with stg; the default is 'G'
|
-- | by this you can prefix all identifiers with stg; the default is 'G'
|
||||||
@@ -47,13 +47,14 @@ haskPreamble =
|
|||||||
"module GSyntax where",
|
"module GSyntax where",
|
||||||
"",
|
"",
|
||||||
"import GF.GFCC.DataGFCC",
|
"import GF.GFCC.DataGFCC",
|
||||||
"import GF.GFCC.Raw.AbsGFCCRaw",
|
"import GF.GFCC.AbsGFCC",
|
||||||
"----------------------------------------------------",
|
"----------------------------------------------------",
|
||||||
"-- automatic translation from GF to Haskell",
|
"-- automatic translation from GF to Haskell",
|
||||||
"----------------------------------------------------",
|
"----------------------------------------------------",
|
||||||
"",
|
"",
|
||||||
"class Gf a where gf :: a -> Exp",
|
"class Gf a where",
|
||||||
"class Fg a where fg :: Exp -> a",
|
" gf :: a -> Exp",
|
||||||
|
" fg :: Exp -> a",
|
||||||
"",
|
"",
|
||||||
predefInst "GString" "String" "DTr [] (AS s) []",
|
predefInst "GString" "String" "DTr [] (AS s) []",
|
||||||
"",
|
"",
|
||||||
@@ -70,8 +71,7 @@ haskPreamble =
|
|||||||
predefInst gtyp typ patt =
|
predefInst gtyp typ patt =
|
||||||
"newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ " deriving Show" +++++
|
"newtype" +++ gtyp +++ "=" +++ gtyp +++ typ +++ " deriving Show" +++++
|
||||||
"instance Gf" +++ gtyp +++ "where" ++++
|
"instance Gf" +++ gtyp +++ "where" ++++
|
||||||
" gf (" ++ gtyp +++ "s) =" +++ patt +++++
|
" gf (" ++ gtyp +++ "s) =" +++ patt ++++
|
||||||
"instance Fg" +++ gtyp +++ "where" ++++
|
|
||||||
" fg t =" ++++
|
" fg t =" ++++
|
||||||
" case t of" ++++
|
" case t of" ++++
|
||||||
" " +++ patt +++ " ->" +++ gtyp +++ "s" ++++
|
" " +++ patt +++ " ->" +++ gtyp +++ "s" ++++
|
||||||
@@ -81,13 +81,12 @@ type OIdent = String
|
|||||||
|
|
||||||
type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
|
type HSkeleton = [(OIdent, [(OIdent, [OIdent])])]
|
||||||
|
|
||||||
datatypes, gfinstances, fginstances :: (String,HSkeleton) -> String
|
datatypes, gfinstances :: (String,HSkeleton) -> String
|
||||||
datatypes = (foldr (+++++) "") . (filter (/="")) . (map hDatatype) . snd
|
datatypes = (foldr (+++++) "") . (filter (/="")) . (map hDatatype) . snd
|
||||||
gfinstances (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (hInstance m)) g
|
gfinstances (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (gfInstance m)) g
|
||||||
fginstances (m,g) = (foldr (+++++) "") $ (filter (/="")) $ (map (fInstance m)) g
|
|
||||||
|
|
||||||
hDatatype :: (OIdent, [(OIdent, [OIdent])]) -> String
|
hDatatype :: (OIdent, [(OIdent, [OIdent])]) -> String
|
||||||
hInstance, fInstance :: String -> (OIdent, [(OIdent, [OIdent])]) -> String
|
gfInstance :: String -> (OIdent, [(OIdent, [OIdent])]) -> String
|
||||||
|
|
||||||
hDatatype ("Cn",_) = "" ---
|
hDatatype ("Cn",_) = "" ---
|
||||||
hDatatype (cat,[]) = ""
|
hDatatype (cat,[]) = ""
|
||||||
@@ -120,6 +119,7 @@ hDatatypeGADT (cat, rules)
|
|||||||
[ gId f +++ "::" +++ concatMap (\a -> gId a +++ "-> ") args ++ t | (f,args) <- rules ]
|
[ gId f +++ "::" +++ concatMap (\a -> gId a +++ "-> ") args ++ t | (f,args) <- rules ]
|
||||||
where t = "Tree" +++ gId cat ++ "_"
|
where t = "Tree" +++ gId cat ++ "_"
|
||||||
|
|
||||||
|
gfInstance m crs = hInstance m crs ++++ fInstance m crs
|
||||||
|
|
||||||
----hInstance m ("Cn",_) = "" --- seems to belong to an old applic. AR 18/5/2004
|
----hInstance m ("Cn",_) = "" --- seems to belong to an old applic. AR 18/5/2004
|
||||||
hInstance m (cat,[]) = ""
|
hInstance m (cat,[]) = ""
|
||||||
@@ -133,13 +133,12 @@ hInstance m (cat,rules)
|
|||||||
-- no show for GADTs
|
-- no show for GADTs
|
||||||
-- ++++ " gf (" ++ gId cat +++ "xs) = error (\"Bad " ++ cat ++ " value: \" ++ show xs)"
|
-- ++++ " gf (" ++ gId cat +++ "xs) = error (\"Bad " ++ cat ++ " value: \" ++ show xs)"
|
||||||
| otherwise =
|
| otherwise =
|
||||||
"instance Gf" +++ gId cat +++ "where" ++
|
"instance Gf" +++ gId cat +++ "where\n" ++
|
||||||
(if length rules == 1 then "" else "\n") +++
|
unlines [mkInst f xx | (f,xx) <- rules]
|
||||||
foldr1 (\x y -> x ++ "\n" +++ y) [mkInst f xx | (f,xx) <- rules]
|
|
||||||
where
|
where
|
||||||
ec = elemCat cat
|
ec = elemCat cat
|
||||||
baseVars = mkVars (baseSize (cat,rules))
|
baseVars = mkVars (baseSize (cat,rules))
|
||||||
mkInst f xx = let xx' = mkVars (length xx) in "gf " ++
|
mkInst f xx = let xx' = mkVars (length xx) in " gf " ++
|
||||||
(if length xx == 0 then gId f else prParenth (gId f +++ foldr1 (+++) xx')) +++
|
(if length xx == 0 then gId f else prParenth (gId f +++ foldr1 (+++) xx')) +++
|
||||||
"=" +++ mkRHS f xx'
|
"=" +++ mkRHS f xx'
|
||||||
mkVars n = ["x" ++ show i | i <- [1..n]]
|
mkVars n = ["x" ++ show i | i <- [1..n]]
|
||||||
@@ -150,14 +149,13 @@ hInstance m (cat,rules)
|
|||||||
----fInstance m ("Cn",_) = "" ---
|
----fInstance m ("Cn",_) = "" ---
|
||||||
fInstance m (cat,[]) = ""
|
fInstance m (cat,[]) = ""
|
||||||
fInstance m (cat,rules) =
|
fInstance m (cat,rules) =
|
||||||
"instance Fg" +++ gId cat +++ "where" ++++
|
" fg t =" ++++
|
||||||
" fg t =" ++++
|
" case t of" ++++
|
||||||
" case t of" ++++
|
unlines [mkInst f xx | (f,xx) <- rules] ++++
|
||||||
foldr1 (\x y -> x ++ "\n" ++ y) [mkInst f xx | (f,xx) <- rules] ++++
|
" _ -> error (\"no" +++ cat ++ " \" ++ show t)"
|
||||||
" _ -> error (\"no" +++ cat ++ " \" ++ show t)"
|
|
||||||
where
|
where
|
||||||
mkInst f xx =
|
mkInst f xx =
|
||||||
" DTr [] (AC (CId \"" ++ f ++ "\")) " ++
|
" DTr [] (AC (CId \"" ++ f ++ "\")) " ++
|
||||||
"[" ++ prTList "," xx' ++ "]" +++
|
"[" ++ prTList "," xx' ++ "]" +++
|
||||||
"->" +++ mkRHS f xx'
|
"->" +++ mkRHS f xx'
|
||||||
where xx' = ["x" ++ show i | (_,i) <- zip xx [1..]]
|
where xx' = ["x" ++ show i | (_,i) <- zip xx [1..]]
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ endif
|
|||||||
|
|
||||||
all: unix gfdoc $(BUILD_JAR) lib
|
all: unix gfdoc $(BUILD_JAR) lib
|
||||||
|
|
||||||
|
static: GHCFLAGS += -optl-static
|
||||||
|
static: unix
|
||||||
|
|
||||||
|
|
||||||
gf: unix
|
gf: unix
|
||||||
|
|
||||||
unix: today opt
|
unix: today opt
|
||||||
|
|||||||
Reference in New Issue
Block a user