From fc1b51aa95ec9e1a628b1255722202472484ee37 Mon Sep 17 00:00:00 2001 From: Thomas Hallgren Date: Thu, 17 Jan 2019 21:04:08 +0100 Subject: Adding -output-format canonical_gf This output format converts a GF grammar to a "canonical" GF grammar. A canonical GF grammar consists of - one self-contained module for the abstract syntax - one self-contained module per concrete syntax The concrete syntax modules contain param, lincat and lin definitions, everything else has been eliminated by the partial evaluator, including references to resource library modules and functors. Record types and tables are retained. The -output-format canonical_gf option writes canonical GF grammars to a subdirectory "canonical/". The canonical GF grammars are written as normal GF ".gf" source files, which can be compiled with GF in the normal way. The translation to canonical form goes via an AST for canonical GF grammars, defined in GF.Grammar.Canonical. This is a simple, self-contained format that doesn't cover everyting in GF (e.g. omitting dependent types and HOAS), but it is complete enough to translate the Foods and Phrasebook grammars found in gf-contrib. The AST is based on the GF grammar "GFCanonical" presented here: https://github.com/GrammaticalFramework/gf-core/issues/30#issuecomment-453556553 The translation of concrete syntax to canonical form is based on the previously existing translation of concrete syntax to Haskell, implemented in module GF.Compile.ConcreteToHaskell. This module could now be reimplemented and simplified significantly by going via the canonical format. Perhaps exports to other output formats could benefit by going via the canonical format too. There is also the possibility of completing the GFCanonical grammar mentioned above and using GF itself to convert canonical GF grammars to other formats... --- src/compiler/GF/Compile/PGFtoAbstract.hs | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/compiler/GF/Compile/PGFtoAbstract.hs (limited to 'src/compiler/GF/Compile/PGFtoAbstract.hs') diff --git a/src/compiler/GF/Compile/PGFtoAbstract.hs b/src/compiler/GF/Compile/PGFtoAbstract.hs new file mode 100644 index 000000000..032a53f81 --- /dev/null +++ b/src/compiler/GF/Compile/PGFtoAbstract.hs @@ -0,0 +1,42 @@ +-- | Extract the abstract syntax from a PGF and convert to it +-- the AST for canonical GF grammars +module GF.Compile.PGFtoAbstract(abstract2canonical) where +import qualified Data.Map as M +import PGF(CId,mkCId,showCId,wildCId,unType,abstractName) +import PGF.Internal(abstract,cats,funs) +import GF.Grammar.Canonical + + +abstract2canonical pgf = Abstract (gId (abstractName pgf)) cs fs + where + abstr = abstract pgf + cs = [CatDef (gId c) (convHs' hs) | + (c,(hs,_,_)) <- M.toList (cats abstr), + c `notElem` predefCat] + fs = [FunDef (gId f) (convT ty) | (f,(ty,ar,_,_)) <- M.toList (funs abstr)] + +predefCat = map mkCId ["Float","Int","String"] + +convHs' = map convH' +convH' (bt,name,ty) = + case unType ty of + ([],name,[]) -> gId name -- !! + +convT t = + case unType t of + (hypos,name,[]) -> Type (convHs hypos) (TypeApp (gId name) []) -- !! + +convHs = map convH + +convH (bt,name,ty) = TypeBinding (gId name) (convT ty) + +-------------------------------------------------------------------------------- + +class FromCId i where gId :: CId -> i + +instance FromCId FunId where gId = FunId . showCId +instance FromCId CatId where gId = CatId . showCId +instance FromCId ModId where gId = ModId . showCId + +instance FromCId VarId where + gId i = if i==wildCId then Anonymous else VarId (showCId i) -- cgit v1.2.3