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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
----------------------------------------------------------------------
-- |
-- Maintainer : PL
-- Stability : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/04/16 05:40:49 $
-- > CVS $Author: peb $
-- > CVS $Revision: 1.4 $
--
-- Simplistic GFC format
-----------------------------------------------------------------------------
module GF.Formalism.SimpleGFC where
import Monad (liftM)
import qualified AbsGFC
import qualified Ident
import GF.Formalism.GCFG
import GF.Infra.Print
----------------------------------------------------------------------
-- * basic (leaf) types
type Constr = AbsGFC.CIdent
type Var = Ident.Ident
type Label = AbsGFC.Label
anyVar :: Var
anyVar = Ident.wildIdent
----------------------------------------------------------------------
-- * simple GFC
type SimpleGrammar c n t = Grammar (Decl c) n (LinType c t) (Maybe (Term c t))
type SimpleRule c n t = Rule (Decl c) n (LinType c t) (Maybe (Term c t))
-- ** dependent type declarations
-- | 'Decl x c ts' == x is of type (c applied to ts)
data Decl c = Decl Var c [TTerm]
deriving (Eq, Ord, Show)
data TTerm = Constr :@ [TTerm]
| TVar Var
deriving (Eq, Ord, Show)
decl2cat :: Decl c -> c
decl2cat (Decl _ cat _) = cat
varsInTTerm :: TTerm -> [Var]
varsInTTerm tterm = vars tterm []
where vars (TVar x) = (x:)
vars (_ :@ ts) = foldr (.) id $ map vars ts
tterm2term :: TTerm -> Term c t
tterm2term (con :@ terms) = con :^ map tterm2term terms
tterm2term (TVar x) = Var x
term2tterm :: Term c t -> TTerm
term2tterm (con :^ terms) = con :@ map term2tterm terms
term2tterm (Var x) = TVar x
term2tterm term = error $ "term2tterm: illegal term"
-- ** linearization types and terms
data LinType c t = RecT [(Label, LinType c t)]
| TblT (LinType c t) (LinType c t)
| ConT Constr [Term c t]
| StrT
deriving (Eq, Ord, Show)
isBaseType :: LinType c t -> Bool
isBaseType (ConT _ _) = True
isBaseType (StrT) = True
isBaseType _ = False
data Term c t
= Arg Int c (Path c t) -- ^ argument variable, the 'Path' is a path
-- pointing into the term
| Constr :^ [Term c t] -- ^ constructor
| Rec [(Label, Term c t)] -- ^ record
| Term c t :. Label -- ^ record projection
| Tbl [(Term c t, Term c t)] -- ^ table of patterns\/terms
| Term c t :! Term c t -- ^ table selection
| Variants [Term c t] -- ^ variants
| Term c t :++ Term c t -- ^ concatenation
| Token t -- ^ single token
| Empty -- ^ empty string
| Wildcard -- ^ wildcard pattern variable
| Var Var -- ^ bound pattern variable
-- Res CIdent -- ^ resource identifier
-- Int Integer -- ^ integer
deriving (Eq, Ord, Show)
-- ** calculations on terms
(+.) :: Term c t -> Label -> Term c t
Variants terms +. lbl = variants $ map (+. lbl) terms
Rec record +. lbl = maybe err id $ lookup lbl record
where err = error $ "(+.): label not in record"
Arg arg cat path +. lbl = Arg arg cat (path ++. lbl)
term +. lbl = term :. lbl
(+!) :: (Eq c, Eq t) => Term c t -> Term c t -> Term c t
Variants terms +! pat = variants $ map (+! pat) terms
term +! Variants pats = variants $ map (term +!) pats
term +! arg@(Arg _ _ _) = term :! arg
Arg arg cat path +! pat = Arg arg cat (path ++! pat)
-- cannot handle tables with pattern variales or wildcards (yet):
term@(Tbl table) +! pat = maybe (term :! pat) id $ lookup pat table
term +! pat = term :! pat
(?++) :: Term c t -> Term c t -> Term c t
Variants terms ?++ term = variants $ map (?++ term) terms
term ?++ Variants terms = variants $ map (term ?++) terms
Empty ?++ term = term
term ?++ Empty = term
term1 ?++ term2 = term1 :++ term2
variants :: [Term c t] -> Term c t
variants terms0 = case concatMap flatten terms0 of
[term] -> term
terms -> Variants terms
where flatten (Variants ts) = ts
flatten t = [t]
-- ** enumerations
enumerateTerms :: (Eq c, Eq t) => Maybe (Term c t) -> LinType c t -> [Term c t]
enumerateTerms arg (StrT) = maybe err return arg
where err = error "enumeratePatterns: parameter type should not be string"
enumerateTerms arg (ConT _ terms) = terms
enumerateTerms arg (RecT rtype)
= liftM Rec $ mapM enumAssign rtype
where enumAssign (lbl, ctype) = liftM ((,) lbl) $ enumerateTerms arg ctype
enumerateTerms arg (TblT ptype ctype)
= liftM Tbl $ mapM enumCase $ enumeratePatterns ptype
where enumCase pat = liftM ((,) pat) $ enumerateTerms (fmap (+! pat) arg) ctype
enumeratePatterns :: (Eq c, Eq t) => LinType c t -> [Term c t]
enumeratePatterns t = enumerateTerms Nothing t
----------------------------------------------------------------------
-- * paths of record projections and table selections
newtype Path c t = Path [Either Label (Term c t)] deriving (Eq, Ord, Show)
emptyPath :: Path c t
emptyPath = Path []
-- ** calculations on paths
(++.) :: Path c t -> Label -> Path c t
Path path ++. lbl = Path (Left lbl : path)
(++!) :: Path c t -> Term c t -> Path c t
Path path ++! sel = Path (Right sel : path)
lintypeFollowPath :: Path c t -> LinType c t -> LinType c t
lintypeFollowPath (Path path) = follow path
where follow [] ctype = ctype
follow (Right pat : path) (TblT _ ctype) = follow path ctype
follow (Left lbl : path) (RecT rec)
= maybe err (follow path) $ lookup lbl rec
where err = error $ "lintypeFollowPath: label not in record type"
termFollowPath :: (Eq c, Eq t) => Path c t -> Term c t -> Term c t
termFollowPath (Path path) = follow (reverse path)
where follow [] term = term
follow (Right pat : path) term = follow path (term +! pat)
follow (Left lbl : path) term = follow path (term +. lbl)
lintype2paths :: (Eq c, Eq t) => Path c t -> LinType c t -> [Path c t]
lintype2paths path (ConT _ _) = []
lintype2paths path (StrT) = [ path ]
lintype2paths path (RecT rec) = concat [ lintype2paths (path ++. lbl) ctype |
(lbl, ctype) <- rec ]
lintype2paths path (TblT pt vt) = concat [ lintype2paths (path ++! pat) vt |
pat <- enumeratePatterns pt ]
----------------------------------------------------------------------
instance Print c => Print (Decl c) where
prt (Decl var cat args)
| null args = prVar ++ prt cat
| otherwise = "(" ++ prVar ++ prt cat ++ prtBefore " " args ++ ")"
where prVar | var == anyVar = ""
| otherwise = "?" ++ prt var ++ ":"
instance Print TTerm where
prt (con :@ args)
| null args = prt con
| otherwise = "(" ++ prt con ++ prtBefore " " args ++ ")"
prt (TVar var) = "?" ++ prt var
instance (Print c, Print t) => Print (LinType c t) where
prt (RecT rec) = "{" ++ prtPairList ":" "; " rec ++ "}"
prt (TblT t1 t2) = "(" ++ prt t1 ++ " => " ++ prt t2 ++ ")"
prt (ConT t ts) = prt t ++ "[" ++ prtSep "|" ts ++ "]"
prt (StrT) = "Str"
instance (Print c, Print t) => Print (Term c t) where
prt (Arg n c p) = prt c ++ prt n ++ prt p
prt (c :^ []) = prt c
prt (c :^ ts) = "(" ++ prt c ++ prtBefore " " ts ++ ")"
prt (Rec rec) = "{" ++ prtPairList "=" "; " rec ++ "}"
prt (Tbl tbl) = "[" ++ prtPairList "=>" "; " tbl ++ "]"
prt (Variants ts) = "{| " ++ prtSep " | " ts ++ " |}"
prt (t1 :++ t2) = prt t1 ++ "++" ++ prt t2
prt (Token t) = "'" ++ prt t ++ "'"
prt (Empty) = "[]"
prt (Wildcard) = "_"
prt (term :. lbl) = prt term ++ "." ++ prt lbl
prt (term :! sel) = prt term ++ "!" ++ prt sel
prt (Var var) = "?" ++ prt var
instance (Print c, Print t) => Print (Path c t) where
prt (Path path) = concatMap prtEither (reverse path)
where prtEither (Left lbl) = "." ++ prt lbl
prtEither (Right patt) = "!" ++ prt patt
|