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
|
module FlatCurry.Goodies where
import FlatCurry.Types
type Update a b = (b -> b) -> a -> a
trProg :: (String -> [String] -> [TypeDecl] -> [FuncDecl] -> [OpDecl] -> a)
-> Prog -> a
trProg prog (Prog name imps types funcs ops) = prog name imps types funcs ops
progName :: Prog -> String
progName = trProg (\name _ _ _ _ -> name)
progImports :: Prog -> [String]
progImports = trProg (\_ imps _ _ _ -> imps)
progTypes :: Prog -> [TypeDecl]
progTypes = trProg (\_ _ types _ _ -> types)
progFuncs :: Prog -> [FuncDecl]
progFuncs = trProg (\_ _ _ funcs _ -> funcs)
progOps :: Prog -> [OpDecl]
progOps = trProg (\_ _ _ _ ops -> ops)
updProg :: (String -> String) ->
([String] -> [String]) ->
([TypeDecl] -> [TypeDecl]) ->
([FuncDecl] -> [FuncDecl]) ->
([OpDecl] -> [OpDecl]) -> Prog -> Prog
updProg fn fi ft ff fo = trProg prog
where
prog name imps types funcs ops
= Prog (fn name) (fi imps) (ft types) (ff funcs) (fo ops)
updProgName :: Update Prog String
updProgName f = updProg f id id id id
updProgImports :: Update Prog [String]
updProgImports f = updProg id f id id id
updProgTypes :: Update Prog [TypeDecl]
updProgTypes f = updProg id id f id id
updProgFuncs :: Update Prog [FuncDecl]
updProgFuncs f = updProg id id id f id
updProgOps :: Update Prog [OpDecl]
updProgOps = updProg id id id id
allVarsInProg :: Prog -> [VarIndex]
allVarsInProg = concatMap allVarsInFunc . progFuncs
updProgExps :: Update Prog Expr
updProgExps = updProgFuncs . map . updFuncBody
rnmAllVarsInProg :: Update Prog VarIndex
rnmAllVarsInProg = updProgFuncs . map . rnmAllVarsInFunc
updQNamesInProg :: Update Prog QName
updQNamesInProg f = updProg id id
(map (updQNamesInType f)) (map (updQNamesInFunc f)) (map (updOpName f))
rnmProg :: String -> Prog -> Prog
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,Kind)] -> [ConsDecl] -> a) ->
(QName -> Visibility -> [(TVarIndex,Kind)] -> TypeExpr -> a) ->
(QName -> Visibility -> [(TVarIndex,Kind)] -> NewConsDecl -> 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
trType _ _ typenew (TypeNew name vis params c) = typenew name vis params c
typeName :: TypeDecl -> QName
typeName = trType (\name _ _ _ -> name) (\name _ _ _ -> name) (\name _ _ _ -> name)
typeVisibility :: TypeDecl -> Visibility
typeVisibility = trType (\_ vis _ _ -> vis) (\_ vis _ _ -> vis) (\_ vis _ _ -> vis)
typeParams :: TypeDecl -> [(TVarIndex, Kind)]
typeParams = trType (\_ _ params _ -> params) (\_ _ params _ -> params) (\_ _ params _ -> params)
typeConsDecls :: TypeDecl -> [ConsDecl]
typeConsDecls = trType (\_ _ _ cs -> cs) failed failed
typeSyn :: TypeDecl -> TypeExpr
typeSyn = trType failed (\_ _ _ syn -> syn) failed
isTypeData :: TypeDecl -> Bool
isTypeData = trType (\_ _ _ _ -> True) (\_ _ _ _ -> False) (\_ _ _ _ -> False)
isTypeSyn :: TypeDecl -> Bool
isTypeSyn = trType (\_ _ _ _ -> False) (\_ _ _ _ -> True) (\_ _ _ _ -> False)
isTypeNew :: TypeDecl -> Bool
isTypeNew = trType (\_ _ _ _ -> False) (\_ _ _ _ -> False) (\_ _ _ _ -> True)
updType :: (QName -> QName) ->
(Visibility -> Visibility) ->
([(TVarIndex,Kind)] -> [(TVarIndex,Kind)]) ->
([ConsDecl] -> [ConsDecl]) ->
(NewConsDecl -> NewConsDecl) ->
(TypeExpr -> TypeExpr) -> TypeDecl -> TypeDecl
updType fn fv fp fc fnc fs = trType typ typesyn typenew
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)
typenew name vis params nc = TypeNew (fn name) (fv vis) (fp params) (fnc nc)
updTypeName :: Update TypeDecl QName
updTypeName f = updType f id id id id id
updTypeVisibility :: Update TypeDecl Visibility
updTypeVisibility f = updType id f id id id id
updTypeParams :: Update TypeDecl [(TVarIndex, Kind)]
updTypeParams f = updType id id f id id id
updTypeConsDecls :: Update TypeDecl [ConsDecl]
updTypeConsDecls f = updType id id id f id id
updTypeNewConsDecl :: Update TypeDecl NewConsDecl
updTypeNewConsDecl f = updType id id id id f id
updTypeSynonym :: Update TypeDecl TypeExpr
updTypeSynonym = updType id id id id id
updQNamesInType :: Update TypeDecl QName
updQNamesInType f
= updType f id id (map (updQNamesInConsDecl f)) (updQNamesInNewConsDecl 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))
trNewCons :: (QName -> Visibility -> TypeExpr -> a) -> NewConsDecl -> a
trNewCons cons (NewCons name vis arg) = cons name vis arg
newConsArg :: NewConsDecl -> TypeExpr
newConsArg = trNewCons (\_ _ arg -> arg)
newConsName :: NewConsDecl -> QName
newConsName = trNewCons (\name _ _ -> name)
newConsVisibility :: NewConsDecl -> Visibility
newConsVisibility = trNewCons (\_ vis _ -> vis)
updNewCons :: (QName -> QName) ->
(Visibility -> Visibility) ->
(TypeExpr -> TypeExpr) -> NewConsDecl -> NewConsDecl
updNewCons fn fv fas = trNewCons newcons
where
newcons name vis args = NewCons (fn name) (fv vis) (fas args)
updNewConsName :: Update NewConsDecl QName
updNewConsName f = updNewCons f id id
updNewConsVisibility :: Update NewConsDecl Visibility
updNewConsVisibility f = updNewCons id f id
updNewConsArg :: Update NewConsDecl TypeExpr
updNewConsArg = updNewCons id id
updQNamesInNewConsDecl :: Update NewConsDecl QName
updQNamesInNewConsDecl f = updNewCons f id (updQNamesInTypeExpr f)
tVarIndex :: TypeExpr -> TVarIndex
tVarIndex texpr = case texpr of
(TVar n) -> n
_ -> error "FlatCurryGoodies.tVarIndex: no type variable"
domain :: TypeExpr -> TypeExpr
domain texpr = case texpr of
(FuncType dom _) -> dom
_ -> error "FlatCurryGoodies.domain: no functional type"
range :: TypeExpr -> TypeExpr
range texpr = case texpr of
(FuncType _ ran) -> ran
_ -> error "FlatCurryGoodies.range: no functional type"
tConsName :: TypeExpr -> QName
tConsName texpr = case texpr of
(TCons name _) -> name
_ -> error "FlatCurryGoodies.tConsName: no constructed type"
tConsArgs :: TypeExpr -> [TypeExpr]
tConsArgs texpr = case texpr of
(TCons _ args) -> args
_ -> error "FlatCurryGoodies.tConsArgs: no constructed type"
trTypeExpr :: (TVarIndex -> a) ->
(QName -> [a] -> a) ->
(a -> a -> a) ->
([(TVarIndex, Kind)] -> a -> a) -> TypeExpr -> a
trTypeExpr tvar _ _ _ (TVar tv) = tvar tv
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, Kind)] -> 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 -> Rule -> a) -> FuncDecl -> a
trFunc func (Func name arity vis t rule) = func name arity vis t rule
funcName :: FuncDecl -> QName
funcName = trFunc (\name _ _ _ _ -> name)
funcArity :: FuncDecl -> Int
funcArity = trFunc (\_ arity _ _ _ -> arity)
funcVisibility :: FuncDecl -> Visibility
funcVisibility = trFunc (\_ _ vis _ _ -> vis)
funcType :: FuncDecl -> TypeExpr
funcType = trFunc (\_ _ _ t _ -> t)
funcRule :: FuncDecl -> Rule
funcRule = trFunc (\_ _ _ _ rule -> rule)
updFunc :: (QName -> QName) ->
(Int -> Int) ->
(Visibility -> Visibility) ->
(TypeExpr -> TypeExpr) ->
(Rule -> Rule) -> FuncDecl -> FuncDecl
updFunc fn fa fv ft fr = trFunc func
where
func name arity vis t rule
= Func (fn name) (fa arity) (fv vis) (ft t) (fr rule)
updFuncName :: Update FuncDecl QName
updFuncName f = updFunc f id id id id
updFuncArity :: Update FuncDecl Int
updFuncArity f = updFunc id f id id id
updFuncVisibility :: Update FuncDecl Visibility
updFuncVisibility f = updFunc id id f id id
updFuncType :: Update FuncDecl TypeExpr
updFuncType f = updFunc id id id f id
updFuncRule :: Update FuncDecl Rule
updFuncRule = updFunc id id id id
isExternal :: FuncDecl -> Bool
isExternal = isRuleExternal . funcRule
allVarsInFunc :: FuncDecl -> [Int]
allVarsInFunc = allVarsInRule . funcRule
funcArgs :: FuncDecl -> [Int]
funcArgs = ruleArgs . funcRule
funcBody :: FuncDecl -> Expr
funcBody = ruleBody . funcRule
funcRHS :: FuncDecl -> [Expr]
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 FuncDecl VarIndex
rnmAllVarsInFunc = updFunc id id id id . rnmAllVarsInRule
updQNamesInFunc :: Update FuncDecl QName
updQNamesInFunc f = updFunc f id id (updQNamesInTypeExpr f) (updQNamesInRule f)
updFuncArgs :: Update FuncDecl [VarIndex]
updFuncArgs = updFuncRule . updRuleArgs
updFuncBody :: Update FuncDecl Expr
updFuncBody = updFuncRule . updRuleBody
trRule :: ([Int] -> Expr -> a) -> (String -> a) -> Rule -> a
trRule rule _ (Rule args exp) = rule args exp
trRule _ ext (External s) = ext s
ruleArgs :: Rule -> [Int]
ruleArgs = trRule (\args _ -> args) failed
ruleBody :: Rule -> Expr
ruleBody = trRule (\_ exp -> exp) failed
ruleExtDecl :: Rule -> String
ruleExtDecl = trRule failed id
isRuleExternal :: Rule -> Bool
isRuleExternal = trRule (\_ _ -> False) (\_ -> True)
updRule :: ([Int] -> [Int]) ->
(Expr -> Expr) ->
(String -> String) -> Rule -> Rule
updRule fa fe fs = trRule rule ext
where
rule args exp = Rule (fa args) (fe exp)
ext s = External (fs s)
updRuleArgs :: Update Rule [VarIndex]
updRuleArgs f = updRule f id id
updRuleBody :: Update Rule Expr
updRuleBody f = updRule id f id
updRuleExtDecl :: Update Rule String
updRuleExtDecl f = updRule id id f
allVarsInRule :: Rule -> [Int]
allVarsInRule = trRule (\args body -> args ++ allVars body) (\_ -> [])
rnmAllVarsInRule :: Update Rule VarIndex
rnmAllVarsInRule f = updRule (map f) (rnmAllVars f) id
updQNamesInRule :: Update Rule 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 :: Expr -> Int
varNr expr = case expr of
(Var n) -> n
_ -> error "FlatCurryGoodies.varNr: no variable"
literal :: Expr -> Literal
literal expr = case expr of
(Lit l) -> l
_ -> error "FlatCurryGoodies.literal: no literal"
combType :: Expr -> CombType
combType expr = case expr of
(Comb ct _ _) -> ct
_ -> error "FlatCurryGoodies.combType: no combined expression"
combName :: Expr -> QName
combName expr = case expr of
(Comb _ name _) -> name
_ -> error "FlatCurryGoodies.combName: no combined expression"
combArgs :: Expr -> [Expr]
combArgs expr = case expr of
(Comb _ _ args) -> args
_ -> error "FlatCurryGoodies.combArgs: no combined expression"
missingCombArgs :: Expr -> Int
missingCombArgs = missingArgs . combType
letBinds :: Expr -> [(Int,Expr)]
letBinds expr = case expr of
(Let vs _) -> vs
_ -> error "FlatCurryGoodies.letBinds: no let declaration"
letBody :: Expr -> Expr
letBody expr = case expr of
(Let _ e) -> e
_ -> error "FlatCurryGoodies.letBody: no let declaration"
freeVars :: Expr -> [Int]
freeVars expr = case expr of
(Free vs _) -> vs
_ -> error "FlatCurryGoodies.freeVars: no free variable declaration"
freeExpr :: Expr -> Expr
freeExpr expr = case expr of
(Free _ e) -> e
_ -> error "FlatCurryGoodies.freeExpr: no free variable declaration"
orExps :: Expr -> [Expr]
orExps expr = case expr of
(Or e1 e2) -> [e1,e2]
_ -> error "FlatCurryGoodies.orExps: no or-expression"
caseType :: Expr -> CaseType
caseType expr = case expr of
(Case ct _ _) -> ct
_ -> error "FlatCurryGoodies.caseType: no case expression"
caseExpr :: Expr -> Expr
caseExpr expr = case expr of
(Case _ e _) -> e
_ -> error "FlatCurryGoodies.caseExpr: no case expression"
caseBranches :: Expr -> [BranchExpr]
caseBranches expr = case expr of
(Case _ _ bs) -> bs
_ -> error "FlatCurryGoodies.caseBranches: no case expression"
isVar :: Expr -> Bool
isVar e = case e of
Var _ -> True
_ -> False
isLit :: Expr -> Bool
isLit e = case e of
Lit _ -> True
_ -> False
isComb :: Expr -> Bool
isComb e = case e of
Comb _ _ _ -> True
_ -> False
isLet :: Expr -> Bool
isLet e = case e of
Let _ _ -> True
_ -> False
isFree :: Expr -> Bool
isFree e = case e of
Free _ _ -> True
_ -> False
isOr :: Expr -> Bool
isOr e = case e of
Or _ _ -> True
_ -> False
isCase :: Expr -> Bool
isCase e = case e of
Case _ _ _ -> True
_ -> False
trExpr :: (Int -> a) ->
(Literal -> a) ->
(CombType -> QName -> [a] -> a) ->
([(Int,a)] -> a -> a) ->
([Int] -> a -> a) ->
(a -> a -> a) ->
(CaseType -> a -> [b] -> a) ->
(Pattern -> a -> b) ->
(a -> TypeExpr -> a)
-> Expr -> a
trExpr var _ _ _ _ _ _ _ _ (Var n) = var n
trExpr _ lit _ _ _ _ _ _ _ (Lit l) = lit l
trExpr var lit comb lt fr or cas branch typed (Comb ct name args)
= comb ct name (map (trExpr var lit comb lt fr or cas branch typed) args)
trExpr var lit comb lt fr or cas branch typed (Let bs e)
= lt (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 (Free vs e)
= fr vs (trExpr var lit comb lt fr or cas branch typed e)
trExpr var lit comb lt fr or cas branch typed (Or e1 e2) = or (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 (Case ct e bs)
= cas ct (f e) (map (\ (Branch 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 (Typed e ty)
= typed (trExpr var lit comb lt fr or cas branch typed e) ty
updVars :: (Int -> Expr) -> Expr -> Expr
updVars var = trExpr var Lit Comb Let Free Or Case Branch Typed
updLiterals :: (Literal -> Expr) -> Expr -> Expr
updLiterals lit = trExpr Var lit Comb Let Free Or Case Branch Typed
updCombs :: (CombType -> QName -> [Expr] -> Expr) -> Expr -> Expr
updCombs comb = trExpr Var Lit comb Let Free Or Case Branch Typed
updLets :: ([(Int,Expr)] -> Expr -> Expr) -> Expr -> Expr
updLets lt = trExpr Var Lit Comb lt Free Or Case Branch Typed
updFrees :: ([Int] -> Expr -> Expr) -> Expr -> Expr
updFrees fr = trExpr Var Lit Comb Let fr Or Case Branch Typed
updOrs :: (Expr -> Expr -> Expr) -> Expr -> Expr
updOrs or = trExpr Var Lit Comb Let Free or Case Branch Typed
updCases :: (CaseType -> Expr -> [BranchExpr] -> Expr) -> Expr -> Expr
updCases cas = trExpr Var Lit Comb Let Free Or cas Branch Typed
updBranches :: (Pattern -> Expr -> BranchExpr) -> Expr -> Expr
updBranches branch = trExpr Var Lit Comb Let Free Or Case branch Typed
updTypeds :: (Expr -> TypeExpr -> Expr) -> Expr -> Expr
updTypeds typed = trExpr Var Lit Comb Let Free Or Case Branch typed
isFuncCall :: Expr -> Bool
isFuncCall e = isComb e && isCombTypeFuncCall (combType e)
isFuncPartCall :: Expr -> Bool
isFuncPartCall e = isComb e && isCombTypeFuncPartCall (combType e)
isConsCall :: Expr -> Bool
isConsCall e = isComb e && isCombTypeConsCall (combType e)
isConsPartCall :: Expr -> Bool
isConsPartCall e = isComb e && isCombTypeConsPartCall (combType e)
isGround :: Expr -> Bool
isGround exp
= case exp of
Comb ConsCall _ args -> all isGround args
_ -> isLit exp
allVars :: Expr -> [Int]
allVars e = trExpr (:) (const id) comb lt fr (.) cas branch const e []
where
comb _ _ = foldr (.) id
lt bs exp = exp . foldr (.) id (map (\ (n,ns) -> (n:) . ns) bs)
fr vs exp = (vs++) . exp
cas _ exp bs = exp . foldr (.) id bs
branch pat exp = ((args pat)++) . exp
args pat | isConsPattern pat = patArgs pat
| otherwise = []
rnmAllVars :: Update Expr Int
rnmAllVars f = trExpr (Var . f) Lit Comb lt (Free . map f) Or Case branch Typed
where
lt = Let . map (\ (n,exp) -> (f n,exp))
branch = Branch . updPatArgs (map f)
updQNames :: Update Expr QName
updQNames f = trExpr Var Lit comb Let Free Or Case (Branch . updPatCons f) typed
where
comb ct name args = Comb ct (f name) args
typed e ty = Typed e (updQNamesInTypeExpr f ty)
trBranch :: (Pattern -> Expr -> a) -> BranchExpr -> a
trBranch branch (Branch pat exp) = branch pat exp
branchPattern :: BranchExpr -> Pattern
branchPattern = trBranch (\pat _ -> pat)
branchExpr :: BranchExpr -> Expr
branchExpr = trBranch (\_ e -> e)
updBranch :: (Pattern -> Pattern) -> (Expr -> Expr) -> BranchExpr -> BranchExpr
updBranch fp fe = trBranch branch
where
branch pat exp = Branch (fp pat) (fe exp)
updBranchPattern :: Update BranchExpr Pattern
updBranchPattern f = updBranch f id
updBranchExpr :: Update BranchExpr Expr
updBranchExpr = updBranch id
trPattern :: (QName -> [Int] -> a) -> (Literal -> a) -> Pattern -> a
trPattern pattern _ (Pattern name args) = pattern name args
trPattern _ lpattern (LPattern l) = lpattern l
patCons :: Pattern -> QName
patCons = trPattern (\name _ -> name) failed
patArgs :: Pattern -> [Int]
patArgs = trPattern (\_ args -> args) failed
patLiteral :: Pattern -> Literal
patLiteral = trPattern failed id
isConsPattern :: Pattern -> Bool
isConsPattern = trPattern (\_ _ -> True) (\_ -> False)
updPattern :: (QName -> QName) ->
([Int] -> [Int]) ->
(Literal -> Literal) -> Pattern -> Pattern
updPattern fn fa fl = trPattern pattern lpattern
where
pattern name args = Pattern (fn name) (fa args)
lpattern l = LPattern (fl l)
updPatCons :: (QName -> QName) -> Pattern -> Pattern
updPatCons f = updPattern f id id
updPatArgs :: ([Int] -> [Int]) -> Pattern -> Pattern
updPatArgs f = updPattern id f id
updPatLiteral :: (Literal -> Literal) -> Pattern -> Pattern
updPatLiteral f = updPattern id id f
patExpr :: Pattern -> Expr
patExpr = trPattern (\ name -> Comb ConsCall name . map Var) Lit
|