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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
module AbstractCurry.Build where
import AbstractCurry.Types
infixr 9 ~>
simpleCurryProg :: String -> [String] -> [CTypeDecl] -> [CFuncDecl] -> [COpDecl]
-> CurryProg
simpleCurryProg name imps types funcs ops =
CurryProg name imps Nothing [] [] types funcs ops
simpleCCons :: QName -> CVisibility -> [CTypeExpr] -> CConsDecl
simpleCCons = CCons [] (CContext [])
applyTC :: QName -> [CTypeExpr] -> CTypeExpr
applyTC f es = foldl CTApply (CTCons f) es
(~>) :: CTypeExpr -> CTypeExpr -> CTypeExpr
t1 ~> t2 = CFuncType t1 t2
baseType :: QName -> CTypeExpr
baseType t = CTCons t
listType :: CTypeExpr -> CTypeExpr
listType a = CTApply (CTCons (pre "[]")) a
tupleType :: [CTypeExpr] -> CTypeExpr
tupleType ts
| l==0 = baseType (pre "()")
| l==1 = head ts
| otherwise = foldl CTApply
(CTCons (pre ('(' : take (l-1) (repeat ',') ++ ")")))
ts
where l = length ts
ioType :: CTypeExpr -> CTypeExpr
ioType a = CTApply (CTCons (pre "IO")) a
maybeType :: CTypeExpr -> CTypeExpr
maybeType a = CTApply (CTCons (pre "Maybe")) a
stringType :: CTypeExpr
stringType = baseType (pre "String")
intType :: CTypeExpr
intType = baseType (pre "Int")
floatType :: CTypeExpr
floatType = baseType (pre "Float")
boolType :: CTypeExpr
boolType = baseType (pre "Bool")
charType :: CTypeExpr
charType = baseType (pre "Char")
unitType :: CTypeExpr
unitType = baseType (pre "()")
dateType :: CTypeExpr
dateType = baseType ("Time", "CalendarTime")
emptyClassType :: CTypeExpr -> CQualTypeExpr
emptyClassType te = CQualType (CContext []) te
cfunc :: QName -> Int -> CVisibility -> CQualTypeExpr -> [CRule] -> CFuncDecl
cfunc = CFunc
cmtfunc :: String -> QName -> Int -> CVisibility -> CQualTypeExpr -> [CRule]
-> CFuncDecl
cmtfunc = CmtFunc
stFunc :: QName -> Int -> CVisibility -> CTypeExpr -> [CRule] -> CFuncDecl
stFunc name arity vis texp rs = cfunc name arity vis (emptyClassType texp) rs
stCmtFunc :: String -> QName -> Int -> CVisibility -> CTypeExpr -> [CRule]
-> CFuncDecl
stCmtFunc cm name arity vis texp rs =
cmtfunc cm name arity vis (emptyClassType texp) rs
simpleRule :: [CPattern] -> CExpr -> CRule
simpleRule pats rhs = CRule pats (CSimpleRhs rhs [])
simpleRuleWithLocals :: [CPattern] -> CExpr -> [CLocalDecl] -> CRule
simpleRuleWithLocals pats rhs ldecls = CRule pats (CSimpleRhs rhs ldecls)
guardedRule :: [CPattern] -> [(CExpr,CExpr)] -> [CLocalDecl] -> CRule
guardedRule pats gs ldecls
| length gs == 1 && fst (head gs) == CSymbol (pre "True")
= CRule pats (CSimpleRhs (snd (head gs)) ldecls)
| otherwise = CRule pats (CGuardedRhs gs ldecls)
noGuard :: CExpr -> (CExpr, CExpr)
noGuard e = (CSymbol (pre "True"), e)
applyF :: QName -> [CExpr] -> CExpr
applyF f es = foldl CApply (CSymbol f) es
applyE :: CExpr -> [CExpr] -> CExpr
applyE f args = foldl CApply f args
constF :: QName -> CExpr
constF f = applyF f []
applyV :: CVarIName -> [CExpr] -> CExpr
applyV v es = foldl CApply (CVar v) es
applyJust :: CExpr -> CExpr
applyJust a = applyF (pre "Just") [a]
applyMaybe :: CExpr -> CExpr -> CExpr -> CExpr
applyMaybe a1 a2 a3 = applyF (pre "maybe") [a1,a2,a3]
tupleExpr :: [CExpr] -> CExpr
tupleExpr es | l==0 = constF (pre "()")
| l==1 = head es
| otherwise = applyF (pre ('(' : take (l-1) (repeat ',') ++ ")"))
es
where l = length es
letExpr :: [CLocalDecl] -> CExpr -> CExpr
letExpr locals cexp = if null locals then cexp else CLetDecl locals cexp
cBranch :: CPattern -> CExpr -> (CPattern, CRhs)
cBranch pattern exp = (pattern, CSimpleRhs exp [])
tuplePattern :: [CPattern] -> CPattern
tuplePattern ps
| l==0 = CPComb (pre "()") []
| l==1 = head ps
| otherwise = CPComb (pre ('(' : take (l-1) (repeat ',') ++ ")")) ps
where l = length ps
pVars :: Int -> [CPattern]
pVars n = [CPVar (i,"x"++show i) | i<-[0..n-1]]
pInt :: Int -> CPattern
pInt x = CPLit (CIntc x)
pFloat :: Float -> CPattern
pFloat x = CPLit (CFloatc x)
pChar :: Char -> CPattern
pChar x = CPLit (CCharc x)
pNil :: CPattern
pNil = CPComb (pre "[]") []
listPattern :: [CPattern] -> CPattern
listPattern [] = pNil
listPattern (p:ps) = CPComb (pre ":") [p, listPattern ps]
stringPattern :: String -> CPattern
stringPattern = CPLit . CStringc
list2ac :: [CExpr] -> CExpr
list2ac [] = constF (pre "[]")
list2ac (c:cs) = applyF (pre ":") [c, list2ac cs]
cInt :: Int -> CExpr
cInt x = CLit (CIntc x)
cFloat :: Float -> CExpr
cFloat x = CLit (CFloatc x)
cChar :: Char -> CExpr
cChar x = CLit (CCharc x)
string2ac :: String -> CExpr
string2ac s = CLit (CStringc s)
toVar :: Int -> CExpr
toVar i = CVar (1,"x"++show i)
cvar :: String -> CExpr
cvar s = CVar (1,s)
cpvar :: String -> CPattern
cpvar s = CPVar (1,s)
ctvar :: String -> CTypeExpr
ctvar s = CTVar (1,s)
|