@@ -13,21 +13,19 @@ import Common.Compiler as Compiler (
13
13
Pass ,
14
14
fromString ,
15
15
)
16
- import Common.Identifiers (Identifier (.. ))
17
16
import qualified IR.IR as I
17
+ import qualified IR.MangleNames as I
18
18
19
19
import Common.Pretty (Pretty (.. ))
20
- import Control.Monad (forM , replicateM )
20
+ import Control.Monad (forM )
21
21
import Control.Monad.State.Lazy (
22
22
MonadState ,
23
23
StateT (.. ),
24
- evalStateT ,
25
24
gets ,
26
25
modify ,
27
26
)
28
27
import Data.Bifunctor (Bifunctor (.. ))
29
28
import Data.Foldable (foldrM )
30
- import Data.Functor ((<&>) )
31
29
import qualified Data.Map as M
32
30
import qualified Data.Set as S
33
31
@@ -39,7 +37,6 @@ data CInfo = CInfo
39
37
{ cName :: I. DConId
40
38
, cType :: I. TConId
41
39
, argsType :: [I. Type ]
42
- , cArity :: Int
43
40
}
44
41
deriving (Eq , Show )
45
42
@@ -54,7 +51,7 @@ data TInfo = TInfo
54
51
data DesugarCtx = DesugarCtx
55
52
{ typeMap :: M. Map I. TConId TInfo
56
53
, consMap :: M. Map I. DConId CInfo
57
- , anonCount :: Int
54
+ , symTable :: I. SymTable I. Type
58
55
}
59
56
60
57
@@ -67,24 +64,18 @@ newtype DesugarFn a = DesugarFn (StateT DesugarCtx Compiler.Pass a)
67
64
deriving (MonadState DesugarCtx ) via (StateT DesugarCtx Compiler. Pass )
68
65
69
66
70
- runDesugarFn :: DesugarFn a -> DesugarCtx -> Compiler. Pass a
71
- runDesugarFn (DesugarFn m ) = evalStateT m
67
+ unDesugarFn :: DesugarFn a -> StateT DesugarCtx Compiler. Pass a
68
+ unDesugarFn (DesugarFn a ) = a
72
69
73
70
74
- freshVar :: DesugarFn Identifier
75
- freshVar = do
76
- currCount <- gets anonCount
77
- modify $ \ ctx -> ctx{anonCount = anonCount ctx + 1 }
78
- return $ fromString (" __pat_anon" ++ show currCount)
79
-
80
-
81
- buildCtx :: [(I. TConId , I. TypeDef )] -> DesugarCtx
82
- buildCtx tds =
83
- DesugarCtx
84
- { anonCount = 0
85
- , typeMap = buildTypeMap tds
86
- , consMap = buildConsMap tds
87
- }
71
+ freshVar :: I. Type -> DesugarFn I. VarId
72
+ freshVar t = do
73
+ syms <- gets symTable
74
+ let origin = " __anonymous_pattern"
75
+ name = I. pickId syms origin
76
+ syms' = M. insert name I. SymInfo {I. symOrigin = origin, I. symType = t} syms
77
+ modify $ \ ctx -> ctx{symTable = syms'}
78
+ return name
88
79
89
80
90
81
-- TODO: this should be a pattern synonym
@@ -93,12 +84,12 @@ unreachableExpr = I.Exception $ I.ExceptDefault $ I.LitIntegral 0
93
84
94
85
95
86
desugarPattern :: I. Program I. Type -> Compiler. Pass (I. Program I. Type )
96
- desugarPattern p@ I. Program {I. programDefs = defs, I. typeDefs = tds} =
97
- (`runDesugarFn` buildCtx tds) $ do
98
- defs' <- mapM desugarExprsDefs defs
99
- return $ p{I. programDefs = defs'}
87
+ desugarPattern p@ I. Program {I. programDefs = defs, I. typeDefs = tds, I. symTable = syms} = do
88
+ (defs', symTable -> syms') <- runStateT (unDesugarFn $ mapM desugarExprsDefs defs) initCtx
89
+ return $ p{I. programDefs = defs', I. symTable = syms'}
100
90
where
101
- desugarExprsDefs (vs, es) = desugarExpr es <&> (vs,)
91
+ desugarExprsDefs (vs, es) = (vs,) <$> desugarExpr es
92
+ initCtx = DesugarCtx {typeMap = buildTypeMap tds, consMap = buildConsMap tds, symTable = syms}
102
93
103
94
104
95
desugarExpr :: I. Expr I. Type -> DesugarFn (I. Expr I. Type )
@@ -116,19 +107,19 @@ desugarExpr (I.Match e arms t) = do
116
107
I. Var _ _ -> desugarMatch [e] eqns (unreachableExpr t) -- TODO: add let alias
117
108
_ -> do
118
109
-- Bind scrutinee to a variable before threading it through desugarMatch
119
- var <- I. VarId <$> freshVar
120
110
let et = I. extract e
111
+ var <- freshVar et
121
112
I. Let [(I. BindVar var et, e)]
122
113
<$> desugarMatch [I. Var var et] eqns (unreachableExpr t)
123
114
<*> pure et
124
115
desugarExpr e = return e
125
116
126
117
127
- desugarMatch
128
- :: [I. Expr I. Type ]
129
- -> [Equation ]
130
- -> I. Expr I. Type -- Default expression the 'I.Match' should return
131
- -> DesugarFn (I. Expr I. Type )
118
+ desugarMatch ::
119
+ [I. Expr I. Type ] ->
120
+ [Equation ] ->
121
+ I. Expr I. Type -> -- Default expression the 'I.Match' should return
122
+ DesugarFn (I. Expr I. Type )
132
123
desugarMatch [] [] def = return def
133
124
desugarMatch [] (([] , e) : _) _ = return e
134
125
desugarMatch [] _ _ = error " can't happen 1"
@@ -190,27 +181,28 @@ desugarMatchCons (u : us) qs@(q : _) def = do
190
181
getCon ((I. AltData dcon _ _) : _, _) = dcon
191
182
getCon _ = error " can't happen 5"
192
183
getTyp ((I. AltData _ _ t) : _, _) = t
193
- getTyp _ = error " no no no -- Simon Peyton Jones"
194
- makeBinder vid t = I. AltBinder $ I. BindVar vid t
184
+ getTyp _ = error " no no no 555"
195
185
196
186
sameConsAs c = filter ((== c) . getCon) qs
197
187
198
188
desugarArm :: I. DConId -> I. Type -> [Equation ] -> DesugarFn (I. Alt I. Type , I. Expr I. Type )
199
189
desugarArm dcon dconTyp qs' = do
200
- k <- getArity dcon
201
- newIds <- replicateM k freshVar
202
- cinfo <- getCInfo dcon
203
- let newVars = I. VarId <$> newIds
204
- argsTyps = argsType cinfo
205
- bs' = zipWith makeBinder newVars argsTyps
206
- us' = zipWith I. Var newVars argsTyps
207
- qs'' = [(as' ++ as, e) | ((I. AltData _ as' _) : as, e) <- qs']
190
+ argsTyps <- argsType <$> getCInfo dcon
191
+
192
+ (unzip -> (bs', us')) <- forM argsTyps $ \ argTyp -> do
193
+ name <- freshVar argTyp
194
+ return (I. AltBinder $ I. BindVar name argTyp, I. Var name argTyp)
195
+
208
196
body <-
209
197
if null qs'
210
- then -- We're done desugaring this equation, just use default body
198
+ then do
199
+ -- We're done desugaring this equation, just use default body
211
200
return def
212
- else -- Recursively generate body from remaining equations
201
+ else do
202
+ -- Recursively generate body from remaining equations
203
+ let qs'' = [(as' ++ as, e) | ((I. AltData _ as' _) : as, e) <- qs']
213
204
desugarMatch (us' ++ us) qs'' def
205
+
214
206
return (I. AltData dcon bs' dconTyp, body)
215
207
desugarMatchCons _ _ _ = error " can't happen 7"
216
208
@@ -232,12 +224,6 @@ getConstructors dcon = do
232
224
return $ tCSet t
233
225
234
226
235
- getArity :: I. DConId -> DesugarFn Int
236
- getArity dcon = do
237
- c <- getCInfo dcon
238
- return $ cArity c
239
-
240
-
241
227
getCInfo :: I. DConId -> DesugarFn CInfo
242
228
getCInfo dcon = gets (M. lookup dcon . consMap) >>= maybe (desugarError dcon) return
243
229
@@ -269,5 +255,4 @@ buildConsMap = foldr (build . second I.variants) M.empty
269
255
{ cName = dcon
270
256
, cType = tcon
271
257
, argsType = variantTypes typs
272
- , cArity = length $ variantTypes typs
273
258
}
0 commit comments