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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
|
module FlatCurry.Annotated.Goodies where
import FlatCurry.Annotated.Types
import qualified FlatCurry.Types as FC
type Update a b = (b -> b) -> a -> a
trProg :: (String -> [String] -> [TypeDecl] -> [AFuncDecl a] -> [OpDecl] -> b)
-> AProg a -> b
trProg f (AProg name imps types funcs ops) = f name imps types funcs ops
progName :: AProg _ -> String
progName = trProg (\name _ _ _ _ -> name)
progImports :: AProg _ -> [String]
progImports = trProg (\_ imps _ _ _ -> imps)
progTypes :: AProg _ -> [TypeDecl]
progTypes = trProg (\_ _ types _ _ -> types)
progFuncs :: AProg a -> [AFuncDecl a]
progFuncs = trProg (\_ _ _ funcs _ -> funcs)
progOps :: AProg _ -> [OpDecl]
progOps = trProg (\_ _ _ _ ops -> ops)
updProg :: (String -> String)
-> ([String] -> [String])
-> ([TypeDecl] -> [TypeDecl])
-> ([AFuncDecl a] -> [AFuncDecl a])
-> ([OpDecl] -> [OpDecl])
-> AProg a -> AProg a
updProg fn fi ft ff fo = trProg prog
where
prog name imps types funcs ops
= AProg (fn name) (fi imps) (ft types) (ff funcs) (fo ops)
updProgName :: Update (AProg _) String
updProgName f = updProg f id id id id
updProgImports :: Update (AProg _) [String]
updProgImports f = updProg id f id id id
updProgTypes :: Update (AProg _) [TypeDecl]
updProgTypes f = updProg id id f id id
updProgFuncs :: Update (AProg a) [AFuncDecl a]
updProgFuncs f = updProg id id id f id
updProgOps :: Update (AProg _) [OpDecl]
updProgOps = updProg id id id id
allVarsInProg :: AProg _ -> [VarIndex]
allVarsInProg = concatMap allVarsInFunc . progFuncs
updProgExps :: Update (AProg a) (AExpr a)
updProgExps = updProgFuncs . map . updFuncBody
rnmAllVarsInProg :: Update (AProg _) VarIndex
rnmAllVarsInProg = updProgFuncs . map . rnmAllVarsInFunc
updQNamesInProg :: Update (AProg _) QName
updQNamesInProg f = updProg id id
(map (updQNamesInType f)) (map (updQNamesInFunc f)) (map (updOpName f))
rnmProg :: String -> AProg a -> AProg a
rnmProg name p = updProgName (const name) (updQNamesInProg rnm p)
where
rnm (mod,n) | mod==progName p = (name,n)
| otherwise = (mod,n)
trType :: (QName -> Visibility -> [TVarIndex] -> [ConsDecl] -> a) ->
(QName -> Visibility -> [TVarIndex] -> TypeExpr -> a) -> TypeDecl -> a
trType typ _ (Type name vis params cs) = typ name vis params cs
trType _ typesyn (TypeSyn name vis params syn) = typesyn name vis params syn
typeName :: TypeDecl -> QName
typeName = trType (\name _ _ _ -> name) (\name _ _ _ -> name)
typeVisibility :: TypeDecl -> Visibility
typeVisibility = trType (\_ vis _ _ -> vis) (\_ vis _ _ -> vis)
typeParams :: TypeDecl -> [TVarIndex]
typeParams = trType (\_ _ params _ -> params) (\_ _ params _ -> params)
typeConsDecls :: TypeDecl -> [ConsDecl]
typeConsDecls = trType (\_ _ _ cs -> cs) failed
typeSyn :: TypeDecl -> TypeExpr
typeSyn = trType failed (\_ _ _ syn -> syn)
isTypeSyn :: TypeDecl -> Bool
isTypeSyn = trType (\_ _ _ _ -> False) (\_ _ _ _ -> True)
updType :: (QName -> QName) ->
(Visibility -> Visibility) ->
([TVarIndex] -> [TVarIndex]) ->
([ConsDecl] -> [ConsDecl]) ->
(TypeExpr -> TypeExpr) -> TypeDecl -> TypeDecl
updType fn fv fp fc fs = trType typ typesyn
where
typ name vis params cs = Type (fn name) (fv vis) (fp params) (fc cs)
typesyn name vis params syn = TypeSyn (fn name) (fv vis) (fp params) (fs syn)
updTypeName :: Update TypeDecl QName
updTypeName f = updType f id id id id
updTypeVisibility :: Update TypeDecl Visibility
updTypeVisibility f = updType id f id id id
updTypeParams :: Update TypeDecl [TVarIndex]
updTypeParams f = updType id id f id id
updTypeConsDecls :: Update TypeDecl [ConsDecl]
updTypeConsDecls f = updType id id id f id
updTypeSynonym :: Update TypeDecl TypeExpr
updTypeSynonym = updType id id id id
updQNamesInType :: Update TypeDecl QName
updQNamesInType f
= updType f id id (map (updQNamesInConsDecl f)) (updQNamesInTypeExpr f)
trCons :: (QName -> Int -> Visibility -> [TypeExpr] -> a) -> ConsDecl -> a
trCons cons (Cons name arity vis args) = cons name arity vis args
consName :: ConsDecl -> QName
consName = trCons (\name _ _ _ -> name)
consArity :: ConsDecl -> Int
consArity = trCons (\_ arity _ _ -> arity)
consVisibility :: ConsDecl -> Visibility
consVisibility = trCons (\_ _ vis _ -> vis)
consArgs :: ConsDecl -> [TypeExpr]
consArgs = trCons (\_ _ _ args -> args)
updCons :: (QName -> QName) ->
(Int -> Int) ->
(Visibility -> Visibility) ->
([TypeExpr] -> [TypeExpr]) -> ConsDecl -> ConsDecl
updCons fn fa fv fas = trCons cons
where
cons name arity vis args = Cons (fn name) (fa arity) (fv vis) (fas args)
updConsName :: Update ConsDecl QName
updConsName f = updCons f id id id
updConsArity :: Update ConsDecl Int
updConsArity f = updCons id f id id
updConsVisibility :: Update ConsDecl Visibility
updConsVisibility f = updCons id id f id
updConsArgs :: Update ConsDecl [TypeExpr]
updConsArgs = updCons id id id
updQNamesInConsDecl :: Update ConsDecl QName
updQNamesInConsDecl f = updCons f id id (map (updQNamesInTypeExpr f))
tVarIndex :: TypeExpr -> TVarIndex
tVarIndex texpr = case texpr of
(TVar n) -> n
_ -> error "AnnotatedFlatCurryGoodies.tVarIndex: no type variable"
domain :: TypeExpr -> TypeExpr
domain texpr = case texpr of
(FuncType dom _) -> dom
_ -> error "AnnotatedFlatCurryGoodies.domain: no functional type"
range :: TypeExpr -> TypeExpr
range texpr = case texpr of
(FuncType _ ran) -> ran
_ -> error "AnnotatedFlatCurryGoodies.range: no functional type"
tConsName :: TypeExpr -> QName
tConsName texpr = case texpr of
(TCons name _) -> name
_ -> error "AnnotatedFlatCurryGoodies.tConsName: no functional type"
tConsArgs :: TypeExpr -> [TypeExpr]
tConsArgs texpr = case texpr of
(TCons _ args) -> args
_ -> error "AnnotatedFlatCurryGoodies.tConsArgs: no functional type"
trTypeExpr :: (TVarIndex -> a) ->
(QName -> [a] -> a) ->
(a -> a -> a) ->
([TVarIndex] -> a -> a) -> TypeExpr -> a
trTypeExpr tvar _ _ _ (TVar n) = tvar n
trTypeExpr tvar tcons functype foralltype (TCons name args)
= tcons name (map (trTypeExpr tvar tcons functype foralltype) args)
trTypeExpr tvar tcons functype foralltype (FuncType from to)
= functype (f from) (f to)
where
f = trTypeExpr tvar tcons functype foralltype
trTypeExpr tvar tcons functype foralltype (ForallType ns t)
= foralltype ns (trTypeExpr tvar tcons functype foralltype t)
isTVar :: TypeExpr -> Bool
isTVar = trTypeExpr (\_ -> True) (\_ _ -> False) (\_ _ -> False) (\_ _ -> False)
isTCons :: TypeExpr -> Bool
isTCons
= trTypeExpr (\_ -> False) (\_ _ -> True) (\_ _ -> False) (\_ _ -> False)
isFuncType :: TypeExpr -> Bool
isFuncType
= trTypeExpr (\_ -> False) (\_ _ -> False) (\_ _ -> True) (\_ _ -> False)
isForallType :: TypeExpr -> Bool
isForallType
= trTypeExpr (\_ -> False) (\_ _ -> False) (\_ _ -> False) (\_ _ -> True)
updTVars :: (TVarIndex -> TypeExpr) -> TypeExpr -> TypeExpr
updTVars tvar = trTypeExpr tvar TCons FuncType ForallType
updTCons :: (QName -> [TypeExpr] -> TypeExpr) -> TypeExpr -> TypeExpr
updTCons tcons = trTypeExpr TVar tcons FuncType ForallType
updFuncTypes :: (TypeExpr -> TypeExpr -> TypeExpr) -> TypeExpr -> TypeExpr
updFuncTypes functype = trTypeExpr TVar TCons functype ForallType
updForallTypes :: ([TVarIndex] -> TypeExpr -> TypeExpr) -> TypeExpr -> TypeExpr
updForallTypes = trTypeExpr TVar TCons FuncType
argTypes :: TypeExpr -> [TypeExpr]
argTypes (TVar _) = []
argTypes (TCons _ _) = []
argTypes (FuncType dom ran) = dom : argTypes ran
argTypes (ForallType _ _) = []
resultType :: TypeExpr -> TypeExpr
resultType (TVar n) = TVar n
resultType (TCons name args) = TCons name args
resultType (FuncType _ ran) = resultType ran
resultType (ForallType ns t) = ForallType ns t
rnmAllVarsInTypeExpr :: (TVarIndex -> TVarIndex) -> TypeExpr -> TypeExpr
rnmAllVarsInTypeExpr f = updTVars (TVar . f)
updQNamesInTypeExpr :: (QName -> QName) -> TypeExpr -> TypeExpr
updQNamesInTypeExpr f = updTCons (\name args -> TCons (f name) args)
trOp :: (QName -> Fixity -> Int -> a) -> OpDecl -> a
trOp op (Op name fix prec) = op name fix prec
opName :: OpDecl -> QName
opName = trOp (\name _ _ -> name)
opFixity :: OpDecl -> Fixity
opFixity = trOp (\_ fix _ -> fix)
opPrecedence :: OpDecl -> Int
opPrecedence = trOp (\_ _ prec -> prec)
updOp :: (QName -> QName) ->
(Fixity -> Fixity) ->
(Int -> Int) -> OpDecl -> OpDecl
updOp fn ff fp = trOp op
where
op name fix prec = Op (fn name) (ff fix) (fp prec)
updOpName :: Update OpDecl QName
updOpName f = updOp f id id
updOpFixity :: Update OpDecl Fixity
updOpFixity f = updOp id f id
updOpPrecedence :: Update OpDecl Int
updOpPrecedence = updOp id id
trFunc :: (QName -> Int -> Visibility -> TypeExpr -> ARule a -> b)
-> AFuncDecl a -> b
trFunc func (AFunc name arity vis t rule) = func name arity vis t rule
funcName :: AFuncDecl _ -> QName
funcName = trFunc (\name _ _ _ _ -> name)
funcArity :: AFuncDecl _ -> Int
funcArity = trFunc (\_ arity _ _ _ -> arity)
funcVisibility :: AFuncDecl _ -> Visibility
funcVisibility = trFunc (\_ _ vis _ _ -> vis)
funcType :: AFuncDecl _ -> TypeExpr
funcType = trFunc (\_ _ _ t _ -> t)
funcRule :: AFuncDecl a -> ARule a
funcRule = trFunc (\_ _ _ _ rule -> rule)
updFunc :: (QName -> QName)
-> (Int -> Int)
-> (Visibility -> Visibility)
-> (TypeExpr -> TypeExpr)
-> (ARule a -> ARule a)
-> AFuncDecl a -> AFuncDecl a
updFunc fn fa fv ft fr = trFunc func
where
func name arity vis t rule
= AFunc (fn name) (fa arity) (fv vis) (ft t) (fr rule)
updFuncName :: Update (AFuncDecl _) QName
updFuncName f = updFunc f id id id id
updFuncArity :: Update (AFuncDecl _) Int
updFuncArity f = updFunc id f id id id
updFuncVisibility :: Update (AFuncDecl _) Visibility
updFuncVisibility f = updFunc id id f id id
updFuncType :: Update (AFuncDecl _) TypeExpr
updFuncType f = updFunc id id id f id
updFuncRule :: Update (AFuncDecl a) (ARule a)
updFuncRule = updFunc id id id id
isExternal :: AFuncDecl _ -> Bool
isExternal = isRuleExternal . funcRule
allVarsInFunc :: AFuncDecl _ -> [VarIndex]
allVarsInFunc = allVarsInRule . funcRule
funcArgs :: AFuncDecl a -> [(VarIndex, a)]
funcArgs = ruleArgs . funcRule
funcBody :: AFuncDecl a -> AExpr a
funcBody = ruleBody . funcRule
funcRHS :: AFuncDecl a -> [AExpr a]
funcRHS f | not (isExternal f) = orCase (funcBody f)
| otherwise = []
where
orCase e
| isOr e = concatMap orCase (orExps e)
| isCase e = concatMap orCase (map branchExpr (caseBranches e))
| otherwise = [e]
rnmAllVarsInFunc :: Update (AFuncDecl _) VarIndex
rnmAllVarsInFunc = updFunc id id id id . rnmAllVarsInRule
updQNamesInFunc :: Update (AFuncDecl _) QName
updQNamesInFunc f = updFunc f id id (updQNamesInTypeExpr f) (updQNamesInRule f)
updFuncArgs :: Update (AFuncDecl a) [(VarIndex, a)]
updFuncArgs = updFuncRule . updRuleArgs
updFuncBody :: Update (AFuncDecl a) (AExpr a)
updFuncBody = updFuncRule . updRuleBody
trRule :: (a -> [(VarIndex, a)] -> AExpr a -> b) -> (a -> String -> b)
-> ARule a -> b
trRule rule _ (ARule a vs e) = rule a vs e
trRule _ ext (AExternal a s) = ext a s
ruleArgs :: ARule a -> [(VarIndex, a)]
ruleArgs = trRule (\_ vs _ -> vs) failed
ruleBody :: ARule a -> AExpr a
ruleBody = trRule (\_ _ e -> e) failed
ruleExtDecl :: ARule _ -> String
ruleExtDecl = trRule failed (\_ s -> s)
isRuleExternal :: ARule _ -> Bool
isRuleExternal = trRule (\_ _ _ -> False) (\_ _ -> True)
updRule :: (a -> a)
-> ([(VarIndex, a)] -> [(VarIndex, a)])
-> (AExpr a -> AExpr a)
-> (String -> String)
-> ARule a -> ARule a
updRule fa fvs fe fs = trRule rule ext
where
rule a vs e = ARule (fa a) (fvs vs) (fe e)
ext a s = AExternal (fa a) (fs s)
updRuleArgs :: Update (ARule a) [(VarIndex, a)]
updRuleArgs f = updRule id f id id
updRuleBody :: Update (ARule a) (AExpr a)
updRuleBody f = updRule id id f id
updRuleExtDecl :: Update (ARule _) String
updRuleExtDecl f = updRule id id id f
allVarsInRule :: ARule _ -> [VarIndex]
allVarsInRule = trRule (\_ vs e -> map fst vs ++ allVars e) (\_ _ -> [])
rnmAllVarsInRule :: Update (ARule _) VarIndex
rnmAllVarsInRule f = updRule id (map (\(v,a) -> (f v, a))) (rnmAllVars f) id
updQNamesInRule :: Update (ARule _) QName
updQNamesInRule = updRuleBody . updQNames
trCombType :: a -> (Int -> a) -> a -> (Int -> a) -> CombType -> a
trCombType fc _ _ _ FuncCall = fc
trCombType _ fpc _ _ (FuncPartCall n) = fpc n
trCombType _ _ cc _ ConsCall = cc
trCombType _ _ _ cpc (ConsPartCall n) = cpc n
isCombTypeFuncCall :: CombType -> Bool
isCombTypeFuncCall = trCombType True (\_ -> False) False (\_ -> False)
isCombTypeFuncPartCall :: CombType -> Bool
isCombTypeFuncPartCall = trCombType False (\_ -> True) False (\_ -> False)
isCombTypeConsCall :: CombType -> Bool
isCombTypeConsCall = trCombType False (\_ -> False) True (\_ -> False)
isCombTypeConsPartCall :: CombType -> Bool
isCombTypeConsPartCall = trCombType False (\_ -> False) False (\_ -> True)
missingArgs :: CombType -> Int
missingArgs = trCombType 0 id 0 id
varNr :: AExpr _ -> VarIndex
varNr aexpr = case aexpr of
(AVar _ n) -> n
_ -> error "AnnotatedFlatCurryGoodies.varNr: no variable"
literal :: AExpr _ -> Literal
literal aexpr = case aexpr of
(ALit _ l) -> l
_ -> error "AnnotatedFlatCurryGoodies.literal: no literal"
combType :: AExpr _ -> CombType
combType aexpr = case aexpr of
(AComb _ ct _ _) -> ct
_ -> error "AnnotatedFlatCurryGoodies.combType: no combined expression"
combName :: AExpr _ -> QName
combName aexpr = case aexpr of
(AComb _ _ name _) -> fst name
_ -> error "AnnotatedFlatCurryGoodies.combName: no combined expression"
combArgs :: AExpr a -> [AExpr a]
combArgs aexpr = case aexpr of
(AComb _ _ _ args) -> args
_ -> error "AnnotatedFlatCurryGoodies.combArgs: no combined expression"
missingCombArgs :: AExpr _ -> Int
missingCombArgs = missingArgs . combType
letBinds :: AExpr a -> [((VarIndex, a), AExpr a)]
letBinds aexpr = case aexpr of
(ALet _ vs _) -> vs
_ -> error "AnnotatedFlatCurryGoodies.letBinds: no let declaration"
letBody :: AExpr a -> AExpr a
letBody aexpr = case aexpr of
(ALet _ _ e) -> e
_ -> error "AnnotatedFlatCurryGoodies.letBody: no let declaration"
freeVars :: AExpr _ -> [VarIndex]
freeVars aexpr = case aexpr of
(AFree _ vs _) -> map fst vs
_ -> error "AnnotatedFlatCurryGoodies.freeVars: no free variable declaration"
freeExpr :: AExpr a -> AExpr a
freeExpr aexpr = case aexpr of
(AFree _ _ e) -> e
_ -> error "AnnotatedFlatCurryGoodies.freeExpr: no free variable declaration"
orExps :: AExpr a -> [AExpr a]
orExps aexpr = case aexpr of
(AOr _ e1 e2) -> [e1,e2]
_ -> error "AnnotatedFlatCurryGoodies.orExps: no or-expression"
caseType :: AExpr _ -> CaseType
caseType aexpr = case aexpr of
(ACase _ ct _ _) -> ct
_ -> error "AnnotatedFlatCurryGoodies.caseType: no case expression"
caseExpr :: AExpr a -> AExpr a
caseExpr aexpr = case aexpr of
(ACase _ _ e _) -> e
_ -> error "AnnotatedFlatCurryGoodies.caseExpr: no case expression"
caseBranches :: AExpr a -> [ABranchExpr a]
caseBranches aexpr = case aexpr of
(ACase _ _ _ bs) -> bs
_ -> error "AnnotatedFlatCurryGoodies.caseBranches: no case expression"
isVar :: AExpr _ -> Bool
isVar e = case e of
AVar _ _ -> True
_ -> False
isLit :: AExpr _ -> Bool
isLit e = case e of
ALit _ _ -> True
_ -> False
isComb :: AExpr _ -> Bool
isComb e = case e of
AComb _ _ _ _ -> True
_ -> False
isLet :: AExpr _ -> Bool
isLet e = case e of
ALet _ _ _ -> True
_ -> False
isFree :: AExpr _ -> Bool
isFree e = case e of
AFree _ _ _ -> True
_ -> False
isOr :: AExpr _ -> Bool
isOr e = case e of
AOr _ _ _ -> True
_ -> False
isCase :: AExpr _ -> Bool
isCase e = case e of
ACase _ _ _ _ -> True
_ -> False
trExpr :: (a -> VarIndex -> b)
-> (a -> Literal -> b)
-> (a -> CombType -> (QName, a) -> [b] -> b)
-> (a -> [((VarIndex, a), b)] -> b -> b)
-> (a -> [(VarIndex, a)] -> b -> b)
-> (a -> b -> b -> b)
-> (a -> CaseType -> b -> [c] -> b)
-> (APattern a -> b -> c)
-> (a -> b -> TypeExpr -> b)
-> AExpr a -> b
trExpr var _ _ _ _ _ _ _ _ (AVar a n) = var a n
trExpr _ lit _ _ _ _ _ _ _ (ALit a l) = lit a l
trExpr var lit comb lt fr or cas branch typed (AComb a ct name args)
= comb a ct name (map (trExpr var lit comb lt fr or cas branch typed) args)
trExpr var lit comb lt fr or cas branch typed (ALet a bs e)
= lt a (map (\ (n,exp) -> (n,f exp)) bs) (f e)
where
f = trExpr var lit comb lt fr or cas branch typed
trExpr var lit comb lt fr or cas branch typed (AFree a vs e)
= fr a vs (trExpr var lit comb lt fr or cas branch typed e)
trExpr var lit comb lt fr or cas branch typed (AOr a e1 e2) = or a (f e1) (f e2)
where
f = trExpr var lit comb lt fr or cas branch typed
trExpr var lit comb lt fr or cas branch typed (ACase a ct e bs)
= cas a ct (f e) (map (\ (ABranch pat exp) -> branch pat (f exp)) bs)
where
f = trExpr var lit comb lt fr or cas branch typed
trExpr var lit comb lt fr or cas branch typed (ATyped a e ty)
= typed a (trExpr var lit comb lt fr or cas branch typed e) ty
updVars :: (a -> VarIndex -> AExpr a) -> AExpr a -> AExpr a
updVars var = trExpr var ALit AComb ALet AFree AOr ACase ABranch ATyped
updLiterals :: (a -> Literal -> AExpr a) -> AExpr a -> AExpr a
updLiterals lit = trExpr AVar lit AComb ALet AFree AOr ACase ABranch ATyped
updCombs :: (a -> CombType -> (QName, a) -> [AExpr a] -> AExpr a)
-> AExpr a -> AExpr a
updCombs comb = trExpr AVar ALit comb ALet AFree AOr ACase ABranch ATyped
updLets :: (a -> [((VarIndex, a), AExpr a)] -> AExpr a -> AExpr a) -> AExpr a -> AExpr a
updLets lt = trExpr AVar ALit AComb lt AFree AOr ACase ABranch ATyped
updFrees :: (a -> [(VarIndex, a)] -> AExpr a -> AExpr a) -> AExpr a -> AExpr a
updFrees fr = trExpr AVar ALit AComb ALet fr AOr ACase ABranch ATyped
updOrs :: (a -> AExpr a -> AExpr a -> AExpr a) -> AExpr a -> AExpr a
updOrs or = trExpr AVar ALit AComb ALet AFree or ACase ABranch ATyped
updCases :: (a -> CaseType -> AExpr a -> [ABranchExpr a] -> AExpr a)
-> AExpr a -> AExpr a
updCases cas = trExpr AVar ALit AComb ALet AFree AOr cas ABranch ATyped
updBranches :: (APattern a -> AExpr a -> ABranchExpr a) -> AExpr a -> AExpr a
updBranches branch = trExpr AVar ALit AComb ALet AFree AOr ACase branch ATyped
updTypeds :: (a -> AExpr a -> TypeExpr -> AExpr a) -> AExpr a -> AExpr a
updTypeds typed = trExpr AVar ALit AComb ALet AFree AOr ACase ABranch typed
isFuncCall :: AExpr _ -> Bool
isFuncCall e = isComb e && isCombTypeFuncCall (combType e)
isFuncPartCall :: AExpr _ -> Bool
isFuncPartCall e = isComb e && isCombTypeFuncPartCall (combType e)
isConsCall :: AExpr _ -> Bool
isConsCall e = isComb e && isCombTypeConsCall (combType e)
isConsPartCall :: AExpr _ -> Bool
isConsPartCall e = isComb e && isCombTypeConsPartCall (combType e)
isGround :: AExpr _ -> Bool
isGround exp = case exp of
AComb _ ConsCall _ args -> all isGround args
_ -> isLit exp
allVars :: AExpr _ -> [VarIndex]
allVars e = trExpr var lit comb lt fr or cas branch typed e []
where
var _ = (:)
lit _ = const id
comb _ _ _ = foldr (.) id
lt _ bs exp = exp . foldr (.) id (map (\ ((n, _), ns) -> (n:) . ns) bs)
fr _ vs exp = (map fst vs ++) . exp
or _ = (.)
cas _ _ exp bs = exp . foldr (.) id bs
branch pat exp = (args pat ++) . exp
args pat | isConsPattern pat = map fst (patArgs pat)
| otherwise = []
typed _ = const
rnmAllVars :: Update (AExpr _) VarIndex
rnmAllVars f = trExpr var ALit AComb lt fre AOr ACase branch ATyped
where
var a = AVar a . f
fre a vs b = AFree a (map (\(v, x) -> (f v, x)) vs) b
lt a = ALet a . map (\ ((n, x), exp) -> ((f n, x),exp))
branch = ABranch . updPatArgs (map (\(v, a) -> (f v, a)))
updQNames :: Update (AExpr _) QName
updQNames f = trExpr AVar ALit comb ALet AFree AOr ACase branch typed
where
comb a ct (name, b) args = AComb a ct (f name, b) args
branch = ABranch . updPatCons f
typed a e ty = ATyped a e (updQNamesInTypeExpr f ty)
trBranch :: (APattern a -> AExpr a -> b) -> ABranchExpr a -> b
trBranch branch (ABranch pat exp) = branch pat exp
branchPattern :: ABranchExpr a -> APattern a
branchPattern = trBranch (\pat _ -> pat)
branchExpr :: ABranchExpr a -> AExpr a
branchExpr = trBranch (\_ e -> e)
updBranch :: (APattern a -> APattern a)
-> (AExpr a -> AExpr a)
-> ABranchExpr a -> ABranchExpr a
updBranch fp fe = trBranch branch
where
branch pat exp = ABranch (fp pat) (fe exp)
updBranchPattern :: Update (ABranchExpr a) (APattern a)
updBranchPattern f = updBranch f id
updBranchExpr :: Update (ABranchExpr a) (AExpr a)
updBranchExpr = updBranch id
trPattern :: (a -> (QName, a) -> [(VarIndex, a)] -> b) -> (a -> Literal -> b)
-> APattern a -> b
trPattern pattern _ (APattern a name args) = pattern a name args
trPattern _ lpattern (ALPattern a l) = lpattern a l
patCons :: APattern _ -> QName
patCons = trPattern (\_ name _ -> fst name) failed
patArgs :: APattern a -> [(VarIndex, a)]
patArgs = trPattern (\_ _ args -> args) failed
patLiteral :: APattern _ -> Literal
patLiteral = trPattern failed (\_ l -> l)
isConsPattern :: APattern _ -> Bool
isConsPattern = trPattern (\ _ _ _ -> True) (\ _ _ -> False)
updPattern :: ((QName, a) -> (QName, a))
-> ([(VarIndex, a)] -> [(VarIndex, a)])
-> (Literal -> Literal)
-> APattern a -> APattern a
updPattern fn fa fl = trPattern apattern alpattern
where
apattern a name args = APattern a (fn name) (fa args)
alpattern a l = ALPattern a (fl l)
updPatCons :: (QName -> QName) -> APattern a -> APattern a
updPatCons f = updPattern (\(n,a) -> (f n, a)) id id
updPatArgs :: ([(VarIndex, a)] -> [(VarIndex, a)]) -> APattern a -> APattern a
updPatArgs f = updPattern id f id
updPatLiteral :: (Literal -> Literal) -> APattern a -> APattern a
updPatLiteral f = updPattern id id f
patExpr :: APattern a -> AExpr a
patExpr = trPattern (\a name vs -> AComb a ConsCall name (map var vs)) ALit
where var (v, a) = AVar a v
annRule :: ARule a -> a
annRule (ARule a _ _) = a
annRule (AExternal a _) = a
annExpr :: AExpr a -> a
annExpr (AComb a _ _ _) = a
annExpr (ACase a _ _ _) = a
annExpr (AVar a _ ) = a
annExpr (ALit a _ ) = a
annExpr (AOr a _ _ ) = a
annExpr (ALet a _ _ ) = a
annExpr (AFree a _ _ ) = a
annExpr (ATyped a _ _ ) = a
annPattern :: APattern a -> a
annPattern (APattern a _ _) = a
annPattern (ALPattern a _ ) = a
unAnnProg :: AProg _ -> FC.Prog
unAnnProg = trProg (\m is ts fs os -> FC.Prog m is ts (map unAnnFuncDecl fs) os)
unAnnFuncDecl :: AFuncDecl _ -> FC.FuncDecl
unAnnFuncDecl = trFunc (\f ar vs ty r -> FC.Func f ar vs ty (unAnnRule r))
unAnnRule :: ARule _ -> FC.Rule
unAnnRule = trRule (\_ vs e -> FC.Rule (map fst vs) (unAnnExpr e))
(\_ s -> FC.External s)
unAnnExpr :: AExpr _ -> FC.Expr
unAnnExpr = trExpr var lit comb lett fre or cse branch typed
where
var _ v = FC.Var v
lit _ l = FC.Lit l
comb _ ct n es = FC.Comb ct (fst n) es
lett _ bs e = FC.Let (map (\((v, _), b) -> (v, b)) bs) e
fre _ vs e = FC.Free (map fst vs) e
or _ a b = FC.Or a b
cse _ ct e bs = FC.Case ct e bs
branch p e = FC.Branch (unAnnPattern p) e
typed _ e ty = FC.Typed e ty
unAnnPattern :: APattern _ -> FC.Pattern
unAnnPattern = trPattern (\_ qn vs -> FC.Pattern (fst qn) (map fst vs))
(\_ l -> FC.LPattern l)
|