summaryrefslogtreecommitdiff
path: root/src/GF/Conversion/Haskell.hs
blob: abe651e1e1a91e88cb86c972c7292e752d1d8e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
----------------------------------------------------------------------
-- |
-- Maintainer  : PL
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/08/11 14:11:46 $ 
-- > CVS $Author: peb $
-- > CVS $Revision: 1.1 $
--
-- Converting/Printing different grammar formalisms in Haskell-readable format
-----------------------------------------------------------------------------


module GF.Conversion.Haskell where

import GF.Formalism.GCFG
import GF.Formalism.SimpleGFC
import GF.Formalism.MCFG
import GF.Formalism.CFG
import GF.Formalism.Utilities
import GF.Conversion.Types
import GF.Data.Operations ((++++), (+++++))
import GF.Infra.Print

import Data.List (intersperse)

-- | SimpleGFC to Haskell
prtSGrammar :: SGrammar -> String
prtSGrammar rules = "-- Simple GFC grammar as a Haskell file" ++++
                    "-- Autogenerated from the Grammatical Framework" +++++
                    "import GF.Formalism.GCFG" ++++
                    "import GF.Formalism.SimpleGFC" ++++
                    "import GF.Formalism.Utilities" ++++
                    "import GF.Canon.AbsGFC (CIdent(..), Label(..))" ++++
                    "import GF.Infra.Ident (Ident(..))" +++++
                    "grammar :: SimpleGrammar Ident (NameProfile Ident) String" ++++
                    "grammar = \n\t[ " ++ 
                    concat (intersperse "\n\t, " (map show rules)) ++ "\n\t]\n\n"

-- | MCFG to Haskell
prtMGrammar :: MGrammar -> String
prtMGrammar rules = "-- Multiple context-free grammar as a Haskell file" ++++
		    "-- Autogenerated from the Grammatical Framework" +++++
		    "import GF.Formalism.GCFG" ++++
		    "import GF.Formalism.MCFG" ++++
                    "import GF.Formalism.Utilities" +++++
                    "grammar :: MCFGrammar String (NameProfile String) String String" ++++
		    "grammar = \n\t[ " ++ 
		    concat (intersperse "\n\t, " (map prtMRule rules)) ++ "\n\t]\n\n" 
    where prtMRule (Rule (Abs cat cats (Name fun profiles)) (Cnc lcat lcats lins)) 
	      = show (Rule (Abs (prt cat) (map prt cats) (Name (prt fun) (map cnvProfile profiles)))
		           (Cnc (map prt lcat) (map (map prt) lcats) (map cnvLin lins)))
	  cnvLin (Lin lbl syms) = Lin (prt lbl) (map (mapSymbol prtMArg id) syms)
	  prtMArg  (cat, lbl, nr) = (prt cat, prt lbl, nr)

-- | CFG to Haskell
prtCGrammar :: CGrammar -> String
prtCGrammar rules = "-- Context-free grammar as a Haskell file" ++++
		    "-- autogenerated from the Grammatical Framework" +++++
		    "import GF.Formalism.CFG" ++++ 
                    "import GF.Formalism.Utilities" ++++
                    "\ngrammar :: CFGrammar String (NameProfile String) String" ++++
		    "grammar = \n\t[ " ++ 
		    concat (intersperse "\n\t, " (map prtCRule rules)) ++ "\n\t]\n\n" 
    where prtCRule (CFRule cat syms (Name fun profiles)) 
	      = show (CFRule (prt cat) (map (mapSymbol prt id) syms) 
	                     (Name (prt fun) (map cnvProfile profiles)))

cnvProfile (Unify args) = Unify args
cnvProfile (Constant forest) = Constant (fmap prt forest)