-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEcore.hs
706 lines (596 loc) · 20.8 KB
/
Ecore.hs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
--
-- Created: 9 Jul 2007 by tobbe@tornkvist.org
-- Desc: Transform the parser output into the form that Epretty.hs expects.
--
module Ecore where
import Data.Char
import Text.ParserCombinators.Parsec
import Ebif
import Bkeep
import Parser
type EAtom = String -- Atom
type EVars = [String] -- List of variable names
type Fname = (String,Arity) -- Function name
data EFname = EFname Fname -- Function name
deriving (Show)
data EMod -- Module definition
= EMod EAtom [Fname] [EFdef]
deriving (Show)
data EFdef -- Named Function definition
= EFdef EFname EFun
deriving (Show)
data EFun -- Local Function definition
= EFun EVars EXpr -- FIXME should it be [EXpr] ?
deriving (Show)
data EClause -- Clause definition
= EClause EPats [EXpr] [EXpr]
deriving (Show)
type EClauses = [EClause] -- List of clauses
type EPats = [EPat] -- List of patterns
data EXpr -- Expression
= EXnil
| EXvar String
| EXnum Integer
| EXpat EPat
| EXatom EAtom
| EXcons EXpr EXpr
| EXtuple [EXpr]
| EXfun EFun
| EXfname Fname
| EXfatbar String
| EXfatbar2 String
| EXcase EVars EClauses
| EXlet EVars [EXpr] EXpr
| EXletRec EVars [EXpr] EXpr
| EXapply EXpr [EXpr]
| EXprimop EAtom [EXpr]
| EXcall EXpr EXpr [EXpr]
deriving (Show)
--
-- Transform the TNT module into an Erlang Core module.
--
tm :: CoreModule -> EMod
tm (m,cps) =
let cps' = myFilter cps
-- FIXME only add if guards are existing
cps'' = cps' ++ [(fatbar), (fatbar2), (fatbar3)]
fnames = [ (cpFname cp, cpArity cp) | cp <- cps'']
cfuns = concatMap mkCurryFuns fnames
-- FIXME add cfuns functions to symtab ?
bk = mkSymtab (bkNew) cps''
fdefs = (map (td bk) cps'') ++ cfuns
in
EMod (lcase m) fnames fdefs
myFilter [] = []
myFilter ([Tsign]:xs) = myFilter xs
myFilter ([ETcon _ _ _]:xs) = myFilter xs
myFilter (x:xs) = x:(myFilter xs)
--
-- Generate curried versions of the original function.
--
mkCurryFuns :: (String,Arity) -> [EFdef]
mkCurryFuns (_,0) = []
mkCurryFuns (fname,arity) = map (mkCurryFun fname) [0..(arity-1)]
mkCurryFun :: String -> Int -> EFdef
mkCurryFun fname arity =
let (vs1,bk) = mkCoreVars arity ([],(bkNew))
(vs2,bk') = mkCoreVars 1 ([],bk)
vs3 = vs1 ++ vs2
in
EFdef
(EFname (fname,arity))
(EFun vs1
(EXfun
(EFun vs2
(EXapply
(EXfname (fname,arity+1))
[EXvar v | v <- vs3]))))
--
-- Transform a function definition
--
td :: Bkeep -> CoreProgram -> EFdef
td bk cp =
let fname = cpFname cp
arity = cpArity cp
fundef = tf bk arity cp
in
EFdef (EFname (fname,arity)) fundef
--
-- Transform each clause of the function def.
--
tf :: Bkeep -> Int -> CoreProgram -> EFun
tf bk arity cp | isUsingGuards cp =
let (vars,bk') = mkCoreVars arity ([],bk)
fbody = tclsG bk' arity vars cp
in
EFun vars fbody
tf bk arity cp =
let (vars,bk') = mkCoreVars arity ([],bk)
clauses = map (tcl bk') cp
in
EFun vars (EXcase vars clauses)
isUsingGuards :: CoreProgram -> Bool
isUsingGuards [] = False
isUsingGuards ((Decl (_, _, [], _)):xs) = isUsingGuards xs
isUsingGuards _ = True
--
-- Transform a clause of a function def. (for funcs. without any guards)
--
tcl :: Bkeep -> TopDecl -> EClause
tcl bk (Decl (_, pats, _, expr)) =
let ps = [epatUcase bk p | HPat p <- pats]
body = te bk expr
in
EClause ps [EXatom "true"] [body]
--
-- Transform a function definition containing guard expressions.
--
-- For each clause, check if the argument pattern matches
-- and if the guard evaluates to 'true'. For the first clause
-- that fulfills these conditions, evaluate the body of
-- that clause.
--
tclsG :: Bkeep -> Int -> EVars -> CoreProgram -> EXpr
--tclsG bk arity vs cp = EXnil -- FIXME
tclsG bk arity vs cp =
let ([v1,v2,v3],bk') = mkCoreVars 3 ([],bk)
gs = map (tg bk' arity) cp
gs' = foldr (\e es -> EXcons e es) EXnil gs
evs = foldr mkCons EPnil [(EPvar w) | w <- vs]
in (EXlet [v1]
[EXlet [v2, v3] [EXpat evs, gs']
(EXapply (EXvar "'_fatbar'/2") [EXvar v2, EXvar v3])]
(EXapply (EXvar v1) []))
mkCons e es = (EPcons e es)
--
-- For each clause, check if it matches the argument and that
-- its guards evaluates to True.
--
-- fatbar _ [] = ErrorPrimop;
-- fatbar [] (x:_) = x;
-- fatbar vs (x:xs) = fatbar2 vs (fatbar3 vs x) xs;
--
-- fatbar2 _ _ [] = ErrorPrimop;
-- fatbar2 _ (True,fun) _ = fun;
-- fatbar2 vs False (x:xs) = fatbar2 vs (fatbar3 vs x) xs;
--
-- fatbar3 [] x = x;
-- fatbar3 (v:vs) x = fatbar3 vs (x v);
--
fatbar :: CoreProgram
fatbar =
[Decl ("_fatbar",[HPat EPdcare,HPat EPnil],[],
EAp (EVar "error") (HPat (EPatom "match_fail"))),
Decl ("_fatbar",[HPat EPnil,HPat (EPcons (EPvar "x") EPdcare)],[],
HPat (EPvar "x")),
Decl ("_fatbar",[HPat (EPvar "vs"),
HPat (EPcons (EPvar "x") (EPvar "xs"))],
[],
EAp (EAp (EAp (HPat (EPvar "_fatbar2"))
(HPat (EPvar "vs")))
(EAp (EAp (HPat (EPvar "_fatbar3"))
(HPat (EPvar "vs"))) (HPat (EPvar "x"))))
(HPat (EPvar "xs")))]
-- (EPapply "x" ["vs"]))
fatbar2 :: CoreProgram
fatbar2 =
[Decl ("_fatbar2",[HPat EPdcare,
HPat (EPtuple [EPtcon "True" [],EPvar "fun"]),
HPat EPdcare],
[],
HPat (EPvar "fun")),
Decl ("_fatbar2",[HPat EPdcare,HPat EPdcare,HPat EPnil],[],
EAp (EVar "error") (HPat (EPatom "match_fail"))),
Decl ("fatbar2",
[HPat (EPvar "vs"),
HPat (EPtcon "False" []),
HPat (EPcons (EPvar "x") (EPvar "xs"))],
[],
EAp (EAp (EAp (HPat (EPvar "_fatbar2")) (HPat (EPvar "vs")))
(EAp (EAp (HPat (EPvar "_fatbar3")) (HPat (EPvar "vs")))
(HPat (EPvar "x"))))
(HPat (EPvar "xs")))]
{-
Decl ("_fatbar2",
[HPat (EPvar "vs"),
HPat (EPtcon "False"),
HPat (EPcons (EPvar "x") (EPvar "xs"))],
[],
EAp (EAp (EAp (HPat (EPvar "_fatbar2")) (HPat (EPvar "vs")))
(EAp (EAp (HPat (EPvar "_fatbar3")) (HPat (EPvar "vs")))
(HPat (EPvar "x"))))
(HPat (EPvar "xs")))]
-}
-- (EPapply "x" ["vs"]))
fatbar3 :: CoreProgram
fatbar3 =
[Decl ("_fatbar3",[HPat EPnil,HPat (EPvar "x")],[],
HPat (EPvar "x")),
Decl ("_fatbar3",[HPat (EPcons (EPvar "v") (EPvar "vs")),
HPat (EPvar "x")],[],
EAp (EAp (HPat (EPvar "_fatbar3")) (HPat (EPvar "vs")))
(EAp (HPat (EPvar "x")) (HPat (EPvar "v"))))]
copies _ 0 = []
copies e n = e:(copies e (n - 1))
-- Generate a function that can test if the pattern matches and
-- if the guard evaluates to true.
--
tg :: Bkeep -> Int -> TopDecl -> EXpr
tg bk arity (Decl (_, pats, [], expr)) =
let ps = [epatUcase bk p | HPat p <- pats]
(vars,bk') = mkCoreVars arity ([],bk)
(v2,bk'') = mkCoreVars 1 ([],bk')
xs = te bk'' expr
dcares = copies EPdcare arity
true = (EXatom "true")
body = (EXcase vars
[(EClause ps [true]
[(EXtuple [true, (EXfun (EFun [] xs))])]),
(EClause dcares [true]
[(EXatom "false")])])
in
tgWrap vars body
tg bk arity (Decl (_, pats, [guard], expr)) =
let ps = [epatUcase bk p | HPat p <- pats]
(vars,bk') = mkCoreVars arity ([],bk)
(v2,bk'') = mkCoreVars 1 ([],bk')
gs = te bk'' guard
xs = te bk'' expr
true = (EXatom "true")
body = (EXcase vars
[(EClause ps [true]
[EXlet v2 [gs]
(EXcase v2
[(EClause [(EPatom "true")] [true]
[(EXtuple [true, (EXfun (EFun [] xs))])]),
(EClause [EPdcare] [true] [(EXatom "false")])])]),
(EClause [EPdcare] [true] [(EXatom "false")])])
in
tgWrap vars body
tgWrap :: EVars -> EXpr -> EXpr
tgWrap [] body = body
tgWrap (v:vs) body =
let b = tgWrap vs body
in
EXfun (EFun [v] b)
{-
EXfun
(EFun vars
(EXcase vars
[(EClause ps [true]
[EXlet v2 [gs]
(EXcase v2
[(EClause [(EPatom "true")] [true]
[(EXtuple [true, (EXfun (EFun [] xs))])]),
(EClause [EPdcare] [true] [(EXatom "false")])])]),
(EClause [EPdcare] [true] [(EXatom "false")])]))
-}
--
-- Transform Expressions
--
te :: Bkeep -> Hxpr -> EXpr
te bk (ENum n) = EXnum n
te bk (ELet False defs expr) =
let vs = [ucase . fst $ v | v <- defs]
xs = map (te bk) [snd x | x <- defs]
ex = te bk expr
in
EXlet vs xs ex
te bk (EIVar v) = EXvar v
te bk x@(EVar v) = tv bk x
te bk x@(HPat p) = toELit bk x
te bk (ELam vs e) =
let expr = te bk e
in EXfun (EFun (map ucase vs) expr)
te bk (EIf b e1 e2) =
let (vars,bk') = mkCoreVars 1 ([],bk)
bx = te bk' b
x1 = te bk' e1
x2 = te bk' e2
c1 = EClause [EPatom "true"] [EXatom "true"] [x1]
c2 = EClause [EPdcare] [EXatom "true"] [x2]
in EXlet vars [bx] (EXcase vars [c1,c2])
te bk (EPrimop name e) =
let expr = map (te bk) e
in EXprimop name expr
te bk (EPapply name vs) =
let evs = [EXvar (ucase v) | v <- vs]
in EXapply (EXvar (ucase name)) evs
te bk (EAp op e2) | isOp op =
let Just (m,f,a) = isBif (var op)
(vars,bk') = mkCoreVars a ([],bk)
evs = [(EXvar v) | v <- vars]
bif = EXcall (EXatom m) (EXatom f) evs
fun = mkCurried vars bif
([v1,v2],bk'') = mkCoreVars 2 ([],bk')
x2 = te bk'' e2
in
EXlet [v1,v2] [x2,fun] (EXapply (EXvar v2) [EXvar v1])
te bk (EAp f e2) | isFun bk f =
let ([v1],bk') = mkCoreVars 1 ([],bk)
x2 = te bk' e2
Just (Sfa fname _) = bkSyFun (var f) bk
fun = EXfname (fname,1)
in
EXlet [v1] [x2] (EXapply fun [(EXvar v1)])
te bk (EAp v1 v2) | isVar bk v1 && isVar bk v2 =
EXapply (EXvar (maybeUcase bk v1))
[(EXvar (maybeUcase bk v2))]
te bk (EAp v e2) | isVar bk v =
let ([v1],bk') = mkCoreVars 1 ([],bk)
x2 = te bk' e2
var = EXvar (maybeUcase bk v)
in
EXlet [v1] [x2] (EXapply var [(EXvar v1)])
te bk (EAp e1 e2) =
let (vars,bk') = mkCoreVars 1 ([],bk)
v1 = head vars
x1 = te bk' e1
x2 = te bk' (EAp (EIVar v1) e2)
in
EXlet vars [x1] x2
--
-- Transform an EVar token
--
tv :: Bkeep -> (Hxpr) -> EXpr
tv bk x@(EVar v) | isFun bk x =
EXapply (EXfname (v,0)) []
tv bk x@(EVar v) | isOp x =
let (vars,bk') = mkCoreVars 2 ([],bk)
evs = [(EXvar w) | w <- vars]
(mod,fun) = mf v
bif = EXcall (EXatom mod) (EXatom fun) evs
in
mkCurried vars bif
tv bk (EVar v) = EXvar $ ucase $ v
mf :: String -> (String,String)
mf v = case (isBif v) of
Just (m,f,a) -> (m,f)
Nothing -> ("erlang",v)
mkCurried :: [String] -> EXpr -> EXpr
mkCurried [] fun = fun
mkCurried (v:vs) fun = EXfun (EFun [v] (mkCurried vs fun))
var :: (Hxpr) -> String
var (EVar v) = v
var (HPat (EPvar v)) = v
--
-- How to generate code for a BIF
--
-- EAp "+" E2
--
-- let a = E2
-- z = \x -> \y -> erlang:'+'(x,y)
-- apply z a
--
-- z = \x -> \y -> erlang:'+'(x,y)
-- let z = EXfun (EFun ["x"]
-- (EXfun (EFun ["y"] (EXcall 'erlang' '+' ["x","y"]))))
--
--
-- How to generate code for a local function
--
-- EAp f E2
--
-- let a = E2
-- apply f a
toELit :: Bkeep -> (Hxpr) -> EXpr
toELit _ (EVar v) = EXvar v
toELit _ (ENum n) = EXnum n
toELit bk (HPat (EPvar v)) = tv bk (EVar v)
toELit bk (HPat p) = EXpat (epatUcase bk p)
epatUcase :: Bkeep -> EPat -> EPat
epatUcase _ EPnil = EPnil
epatUcase bk (EPvar v) = EPvar $ if (isFun bk $ EVar v)
then v
else ucase $ v
epatUcase _ x@(EPnum _) = x
epatUcase _ x@(EPatom _) = x
epatUcase _ EPdcare = EPdcare
epatUcase bk (EPtcon c eps) = EPtcon c (map (epatUcase bk) eps)
epatUcase bk (EPtuple eps) = EPtuple (map (epatUcase bk) eps)
epatUcase bk (EPcons x xs) = EPcons (epatUcase bk x) (epatUcase bk xs)
-- FIXME lookup if the function is known in the symtab instead!!
isFun :: Bkeep -> (Hxpr) -> Bool
isFun bk h | isVar2 h =
let v = getVar h
in
case (bkSyFun v bk) of
Nothing -> False
other -> True
isFun _ _ = False
getVar :: (Hxpr) -> String
getVar (EVar v) = v
getVar (EIVar v) = v
getVar (HPat (EPvar v)) = v
maybeUcase :: Bkeep -> (Hxpr) -> String
maybeUcase bk x@(EVar v) = if (isFun bk x) then v else ucase $ v
maybeUcase bk x@(HPat (EPvar v)) = if (isFun bk x) then v else ucase $ v
maybeUcase _ (EIVar v) = v
isVar :: Bkeep -> (Hxpr) -> Bool
isVar bk x = isVar2 x && not (isFun bk x)
isVar2 :: Hxpr -> Bool
isVar2 (EVar _) = True
isVar2 (EIVar _) = True
isVar2 (HPat (EPvar _)) = True
isVar2 _ = False
isLit :: Hxpr -> Bool
isLit (EVar _) = True
isLit (ENum _) = True
isLit (HPat _) = True
isLit _ = False
primOps :: [String]
primOps = ["+", "-", "*", "/", ">", "<", ">=", "<=",
"==", "=/=", "=:=", "/=", "&&", "||", "error"]
isOp :: Hxpr -> Bool
isOp (EVar op) = elem op primOps
isOp _ = False
mkCoreVars :: Int -> ([String],Bkeep) -> ([String],Bkeep)
mkCoreVars 0 (vars,bk) = (reverse vars,bk)
mkCoreVars arity (vars,bk) | arity > 0 =
let (i,bk') = bkBump bk
var = "_cor" ++ (show i)
in
mkCoreVars (arity - 1) (var:vars, bk')
cpFname :: CoreProgram -> String
cpFname cp = let (Decl p) = head cp
in fst3 p
cpArity :: CoreProgram -> Int
cpArity cp = let (Decl p) = head cp
in length . snd3 $ p
fst3 :: (a,b,c,d) -> a
fst3 (x,_,_,_) = x
snd3 :: (a,b,c,d) -> b
snd3 (_,x,_,_) = x
ucase :: String -> String
ucase = map toUpper
lcase :: String -> String
lcase = map toLower
--
-- Setup the Symbol Table with the info of all locally defined (F/N) functions.
--
mkSymtab :: Bkeep -> [CoreProgram] -> Bkeep
mkSymtab bk [] = bk
mkSymtab bk (cp:cps) =
let bk' = mkSymtab2 bk cp
in mkSymtab bk' cps
mkSymtab2 :: Bkeep -> CoreProgram -> Bkeep
mkSymtab2 bk [] = bk
mkSymtab2 bk ((Decl (name,vs,_,_)):xs) =
mkSymtab2 (bkSyAdd (Sfa name (length vs)) bk) xs
--
-- To test: parser "x"
--
parser input = let scanres = scanner input
pgmres = runParser corePgm () ""
in
case (pgmres scanres) of
Left _ -> []
Right r -> [r]
--
-- Test cases
--
tcAll = map tm [iSingle, iSum, iZip, iZip2, iSingle2, iSum2, isNull,
iSafeHead, iMap, iTsign, iTsign2, iOp, iOp2, iLast,
iLast2]
c1 = tm iSingle
c2 = tm iSum
c3 = tm iZip
c4 = tm iZip2
c5 = tm iSingle2
c6 = tm iSum2
c7 = tm isNull
c8 = tm iSafeHead
c9 = tm iMap
c10 = tm iOp
c11 = tm iOp2
c12 = tm iGuard
c13 = tm iGuard2
c14 = tm iTuple
c15 = tm iFatbar
c16 = tm iGuard3
c17 = tm iGuard4
c18 = tm iTwo
c19 = tm iError
c20 = tm iFatbar2
b1 = dumpSymtab iSingle2
b11 = dumpSymtab iOp2
dumpSymtab :: CoreModule -> Bkeep
dumpSymtab (_,cps) = mkSymtab (bkNew) cps
{-
parseTest corePgm (scanner "module zip where zip [] [] = [];
zip (x:xs) (y:ys) = let z = x+y;
zs = zip xs ys in (z:zs)")
-}
type TestProgram = (Parser.Name, [CoreProgram])
iZip :: TestProgram
iZip = head . parser $ "module Zip where zip [] [] = [];" ++
"zip (x:xs) (y:ys) = let z = x+y;" ++
"zs = zip xs ys in (z:zs);"
iZip2 :: TestProgram
iZip2 = head . parser $ "module Zip where zip _ [] = [];" ++
"zip [] _ = [];" ++
"zip (x:xs) (y:ys) = let z = x+y;" ++
"zs = zip xs ys in (z:zs);"
iSum :: TestProgram
iSum = head . parser $ "module Sum where sum [] = 0;" ++
"sum (x:xs) = let z = sum xs in x + z;"
iSum2 :: TestProgram
iSum2 = head . parser $ "module Sum where sum [] = 0;" ++
"sum (x:xs) = x + sum xs;"
iBool :: TestProgram
iBool = head . parser $ "module Bool where data Bool = True | False;" ++
"add x y = x + y;"
iSingle :: TestProgram
iSingle = head . parser $ "module Single where one = 1;"
iSingle2 :: TestProgram
iSingle2 = head . parser $ "module Single where one = 1;" ++
"two = one + one;"
iLam :: TestProgram
iLam = head . parser $ "module Lam where id = \\x -> x;" ++
"plus1 x = (\\y -> y +1) x;"
iGt :: TestProgram
iGt = head . parser $ "module Gt where gt x y = if (x > y) then 1 else 0;"
isNull :: TestProgram
isNull = head . parser $ "module Null where null [] = True;" ++
"null _ = False;"
isMaybe :: TestProgram
isMaybe = head . parser $ "module Maybe where data Maybe a = Nothing | Just a;"
iSafeHead :: TestProgram
iSafeHead = head . parser $ "module SafeHead where safehead [] = Nothing;" ++
"safehead (x:_) = Just x;"
iMap :: TestProgram
iMap = head . parser $ "module Map where map f [] = [];" ++
"map f (x:xs) = let z = f x;" ++
"zs = map f xs in (z:zs);"
iTsign :: TestProgram
iTsign = head . parser $ "module Tsign where head :: [a] -> a;"
iTsign2 :: TestProgram
iTsign2 = head . parser $ "module X where head :: [a] -> a;" ++
"head (x:_) = x;"
iOp :: TestProgram
iOp = head . parser $ "module X where sum xs = foldl (+) 0 xs;"
iOp2 :: TestProgram
iOp2 = head . parser $ "module X where plus = (+);"
iLast :: TestProgram
iLast = head . parser $ "module X where last [x] = x;" ++
"last (_:xs) = xs;"
iLast2 :: TestProgram
iLast2 = head . parser $ "module X where last [x] = x;"
iGuard :: TestProgram
iGuard = head . parser $ "module X where procString xs | isString xs = True;"
iGuard2 :: TestProgram
iGuard2 = head . parser $ "module X where" ++
"isString (x:xs) | x >= 0 && x <= 255 = isString xs;"
iGuard3 :: TestProgram
iGuard3 = head . parser $ "module X where isZero x | x == 0 = True;" ++
"isZero _ = False;"
iGuard4 :: TestProgram
iGuard4 = head . parser $ "module X where gt x y | x > y = True;" ++
"gt _ _ = False;"
iTuple :: TestProgram
iTuple = head . parser $ "module X where mkTuple x y = (x,y);"
iApply :: TestProgram
iApply = head . parser $ "module X where eee x y z = fff x y z;"
iApply2 :: TestProgram
iApply2 = head . parser $ "module X where xx = 1;" ++
"yy = 2;" ++
"analyze = fatbar 34 xx;"
iFatbar :: TestProgram
iFatbar = head . parser $
"module Fatbar where" ++
"fatbar _ [] = Primop;" ++
"fatbar [] (x:_) = x;" ++
"fatbar vs (x:xs) = fatbar2 vs (fatbar3 vs x) xs;" ++
"fatbar2 _ _ [] = Primop;" ++
"fatbar2 _ (True,fun) _ = fun;" ++
"fatbar2 vs False (x:xs) = fatbar2 vs (fatbar3 vs x) xs;" ++
"fatbar3 [] x = x; fatbar3 (v:vs) x = fatbar3 vs (x v);"
iFatbar2 :: TestProgram
iFatbar2 = head . parser $
"module X where fatbar2 vs False (x:xs) = fatbar2 vs (fatbar3 vs x) xs;"
iList :: TestProgram
iList = head . parser $ "module X where xx = [1,2,3];"
iTwo :: TestProgram
iTwo = head . parser $ "module X where mul x y = x * y; double x = mul x x;"
iError :: TestProgram
iError = head . parser $ "module X where eee x = error x;"