-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExprU.hs
263 lines (233 loc) · 7.06 KB
/
ExprU.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
{-# LANGUAGE DeriveDataTypeable #-}
module ExprU(
module Idx,
Lit(..), PrimOp1(..), PrimOp2(..), AQual(..), Expr(..),
Decl(..),
subst,
renameIdxs,
fv
) where
import Pretty
import Util
import Idx
import Data.Data
import Data.Generics
data Lit =
LiNum Int
| LiLab Int
| LiUnit
deriving (Show,Data,Typeable,Eq)
{- Primitive Operations are CBV builtin functions with
- no special control properties -}
data PrimOp1 =
PrInl
| PrInr
| PrFst
| PrSnd
| PrFix
| PrWNew
| PrSNew
| PrRelease
| PrSRelease
| PrSwap
| PrSSwap
deriving (Show,Data,Typeable,Eq)
data PrimOp2 =
PrPair
| PrPlus
| PrMinus
| PrGt
| PrLt
| PrEqq
| PrOr
| PrAnd
deriving (Show,Data,Typeable,Eq)
data AQual =
AQU
| AQR
| AQA
| AQL
deriving (Show,Data,Typeable,Eq)
data Expr =
ExLit Lit
| ExVar Idx
| ExGVar Idx
| ExAbs AQual Idx Expr
| ExApp Expr Expr
| ExLet Idx Expr Expr
| ExLetp Idx Idx Expr Expr
| ExMatch Expr Idx Expr Idx Expr
| ExWith Expr Expr
| ExDup [Expr] [(Idx,Idx)] Expr
| ExDrop [Expr] Expr
| ExPrim1 PrimOp1 Expr
| ExPrim2 PrimOp2 Expr Expr
deriving (Show,Data,Typeable,Eq)
renameIdxs :: Expr -> [(Idx,Idx)] -> Expr
renameIdxs e ips =
everywhere (mkT rename1Idx) e
where
rename1Idx i1 | (Just i2) <- lookup i1 ips = i2
| otherwise = i1
subst :: Expr -> Idx -> Expr -> Expr
subst e@(ExLit _) _ _ = e
subst (ExVar i) x es
| i == x = es
| otherwise = ExVar i
subst e@(ExGVar g) x es
| g == x = es
| otherwise = ExGVar g
subst e@(ExAbs a i e1) x es
| i == x = e
| otherwise = ExAbs a i (subst e1 x es)
subst (ExApp e1 e2) x es =
ExApp (subst e1 x es) (subst e2 x es)
subst (ExLet i e1 e2) x es
| i == x = ExLet i (subst e1 x es) e2
| otherwise = ExLet i (subst e1 x es) (subst e2 x es)
subst (ExLetp i1 i2 e1 e2) x es
| (i1==x || i2==x) = ExLetp i1 i2 (subst e1 x es) e2
| otherwise = ExLetp i1 i2 (subst e1 x es) (subst e2 x es)
subst (ExMatch e i1 e1 i2 e2) x es
| (i1==x && i2==x) = ExMatch (subst e x es) i1 e1 i2 e2
| (i1==x) = ExMatch (subst e x es) i1 e1 i2 (subst e2 x es)
| (i2==x) = ExMatch (subst e x es) i1 (subst e1 x es) i2 e2
| otherwise = ExMatch (subst e x es)
i1 (subst e1 x es)
i2 (subst e2 x es)
subst (ExWith e1 e2) x es =
ExWith (subst e1 x es) (subst e2 x es)
subst (ExDup el ips e2) x es
| (any pairmatch ips) =
ExDup (map (\e -> subst e x es) el) ips e2
| otherwise =
ExDup (map (\e -> subst e x es) el) ips (subst e2 x es)
where
pairmatch (i1,i2) = (i1 == x || i2 ==x)
subst (ExDrop el e2) x es =
ExDrop (map (\e -> subst e x es) el) (subst e2 x es)
subst (ExPrim1 p e) x es = ExPrim1 p (subst e x es)
subst (ExPrim2 p e1 e2) x es = ExPrim2 p (subst e1 x es) (subst e2 x es)
---- Free Var Calculations ----
unioncat :: [[Idx]] -> [Idx]
unioncat ils = foldl union [] ils
-- SET of free variables
fv :: Expr -> [Idx]
fv (ExVar i) = [i]
fv (ExGVar i) = []
fv (ExLit l) = []
fv (ExPrim1 _ e) = fv e
fv (ExPrim2 _ e1 e2) = fv e1 `union` fv e2
fv (ExAbs aq i e) = delete i (fv e)
fv (ExApp e1 e2) = union (fv e1) (fv e2)
fv (ExLet i e1 e2) = union (fv e1) (delete i (fv e2))
fv (ExLetp i1 i2 e1 e2) = union (fv e1) (fv e2 \\ [i1,i2])
fv (ExMatch e i1 e1 i2 e2) = union (fv e) (union (i1 `delete` fv e1) (i2 `delete` fv e2))
fv (ExWith e1 e2) = fv e1 `union` fv e2
fv (ExDup des newvps e) =
let newvs = (uncurry union) (unzip newvps) in
(unioncat (map fv des)) `union` (fv e \\ newvs)
fv (ExDrop des e) = (unioncat (map fv des)) `union` fv e
---- Declarations ----
data Decl =
DcLet Idx Expr
deriving (Show,Data,Typeable,Eq)
---- Pretty Printing ----
-- Primitive Operator Pretty Printing
instance Pretty PrimOp1 where
pretty (PrInl) = text "Left"
pretty (PrInr) = text "Right"
pretty (PrFst) = text "fst"
pretty (PrSnd) = text "snd"
pretty (PrFix) = text "fix"
pretty (PrWNew) = text "wnew"
pretty (PrSNew) = text "snew"
pretty (PrRelease) = text "release"
pretty (PrSRelease) = text "srelease"
pretty (PrSwap) = text "swap"
pretty (PrSSwap) = text "sswap"
instance Pretty PrimOp2 where
pretty (PrPair) = text "pair"
pretty (PrPlus) = text "+"
pretty (PrMinus) = text "-"
pretty (PrGt) = text ">"
pretty (PrLt) = text "<"
pretty (PrEqq) = text "=="
pretty (PrAnd) = text " and "
pretty (PrOr) = text " or "
instance Pretty AQual where
pretty (AQU) = text "U"
pretty (AQR) = text "R"
pretty (AQA) = text "A"
pretty (AQL) = text "L"
isInfix :: PrimOp2 -> Bool
isInfix PrPair = False
isInfix _ = True
-- precedences: 0 base level, 1 inside an app
instance Pretty Expr where
pretty = pprExprPrec 0
pprIdList :: [Idx] -> Doc
pprIdList is = sep $ punctuate comma (map pretty is)
pprExprList :: [Expr] -> Doc
pprExprList es = sep $ punctuate comma (map pretty es)
pprIdPair :: (Idx,Idx) -> Doc
pprIdPair (i1,i2) = parens (pretty i1 <> comma <> pretty i2)
pprIdPairList :: [(Idx,Idx)] -> Doc
pprIdPairList is = sep $ punctuate comma (map pprIdPair is)
tin = text "in"
instance Pretty Lit where
pretty (LiNum i) = int i
pretty (LiLab i) = text "l"<>int i
pretty (LiUnit) = text "()"
pprExprPrec :: Int -> Expr -> Doc
pprExprPrec p (ExLit l) = pretty l
pprExprPrec p (ExVar i) = pretty i
pprExprPrec p (ExGVar i) = pretty i
pprExprPrec p (ExAbs aq i e) =
let s = (text "\\")<+>(pretty i)<+>(nest 2 (text "-"<>pretty aq<>text ">"</>(pprExprPrec 0 e))) in
addParen p 0 s
pprExprPrec p (ExApp e1 e2) =
let s = (pprExprPrec 1 e1)<+>(pprExprPrec 1 e2) in
addParen p 0 s
pprExprPrec p (ExLet i e1 e2) =
let s = (text "let")<+>(pretty i)<+>equals<+>(pprExprPrec 0 e1)<+>(text "in")
</>(pprExprPrec 0 e2) in
addParen p 0 s
pprExprPrec p (ExLetp i1 i2 e1 e2) =
let s = text "letp"<+>parens (pretty i1<>comma<>pretty i2)
<+>equals<+>pprExprPrec 0 e1<+>tin
</> pprExprPrec 0 e2 in
addParen p 0 s
pprExprPrec p (ExMatch e i1 e1 i2 e2) =
let s = text "case" <+> pprExprPrec 0 e<+> text "of" <> (nest 2 (line <>
text "Left" <+> pretty i1 <+> text "->" <+> pprExprPrec 0 e1
<> text ";"</>
text "Right" <+> pretty i2 <+> text "->" <+> pprExprPrec 0 e2))
in
addParen p 0 s
pprExprPrec p (ExWith e1 e2) =
brackets $ pprExprPrec 0 e1 <> comma <> pprExprPrec 0 e2
pprExprPrec p (ExDup xs xps e) =
let s = text "dup" <+> (pprExprList xs) <+> text "as"
<+> (pprIdPairList xps) <+> tin </> pprExprPrec 0 e
in
addParen p 0 s
pprExprPrec p (ExDrop xs e) =
let s = text "drop" <+> (pprExprList xs) <+> tin
</> pprExprPrec 0 e
in
addParen p 0 s
pprExprPrec p (ExPrim1 pOp e) =
let s = pretty pOp <+> pprExprPrec 0 e in
addParen p 0 s
pprExprPrec p (ExPrim2 pOp e1 e2)
| pOp == PrPair =
parens $ (pprExprPrec 0 e1)<>comma<>(pprExprPrec 0 e2)
| isInfix pOp =
let s = (pprExprPrec 0 e1)<>pretty pOp<>(pprExprPrec 0 e2) in
addParen p 0 s
| otherwise =
let s = pretty pOp <+> (pprExprPrec 1 e1) <+> (pprExprPrec 1 e2) in
addParen p 0 s
instance Pretty Decl where
pretty (DcLet i e) = text("let")<+>(pretty i)<+>equals<+>(pretty e)