-
Notifications
You must be signed in to change notification settings - Fork 134
/
Rewrite.hs
488 lines (409 loc) · 15.3 KB
/
Rewrite.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
-- | This module contains functions for recursively "rewriting"
-- GHC core using "rules".
module Language.Haskell.Liquid.Transforms.Rewrite
( -- * Top level rewrite function
rewriteBinds
-- * Low-level Rewriting Function
-- , rewriteWith
-- * Rewrite Rule
-- , RewriteRule
) where
import Liquid.GHC.API as Ghc hiding (showPpr, substExpr)
import Language.Haskell.Liquid.GHC.TypeRep ()
import Data.Maybe (fromMaybe)
import Control.Monad (msum)
import Control.Monad.State hiding (lift)
import Language.Fixpoint.Misc ({- mapFst, -} mapSnd)
import qualified Language.Fixpoint.Types as F
import Language.Haskell.Liquid.Misc (safeZipWithError, Nat)
import Language.Haskell.Liquid.GHC.Play (substExpr)
import Language.Haskell.Liquid.GHC.Resugar
import Language.Haskell.Liquid.GHC.Misc (unTickExpr, isTupleId, showPpr, mkAlive) -- , showPpr, tracePpr)
import Language.Haskell.Liquid.UX.Config (Config, noSimplifyCore)
import qualified Data.List as L
import qualified Data.HashMap.Strict as M
--------------------------------------------------------------------------------
-- | Top-level rewriter --------------------------------------------------------
--------------------------------------------------------------------------------
rewriteBinds :: Config -> [CoreBind] -> [CoreBind]
rewriteBinds cfg
| simplifyCore cfg
= fmap (normalizeTuples
. rewriteBindWith undollar
. rewriteBindWith tidyTuples
. rewriteBindWith simplifyPatTuple)
| otherwise
= id
simplifyCore :: Config -> Bool
simplifyCore = not . noSimplifyCore
undollar :: RewriteRule
undollar = go
where
go e
| Just (f, a) <- splitDollarApp e
= Just $ App f a
go (Tick t e)
= Tick t <$> go e
go (Let (NonRec x ex) e)
= do ex' <- go ex
e' <- go e
return $ Let (NonRec x ex') e'
go (Let (Rec bes) e)
= Let <$> (Rec <$> mapM goRec bes) <*> go e
go (Case e x t alts)
= Case e x t <$> mapM goAlt alts
go (App e1 e2)
= App <$> go e1 <*> go e2
go (Lam x e)
= Lam x <$> go e
go (Cast e c)
= (`Cast` c) <$> go e
go e
= return e
goRec (x, e)
= (x,) <$> go e
goAlt (Alt c bs e)
= Alt c bs <$> go e
tidyTuples :: RewriteRule
tidyTuples ce = Just $ evalState (go ce) []
where
go (Tick t e)
= Tick t <$> go e
go (Let (NonRec x ex) e)
= do ex' <- go ex
e' <- go e
return $ Let (NonRec x ex') e'
go (Let (Rec bes) e)
= Let <$> (Rec <$> mapM goRec bes) <*> go e
go (Case (Var v) x t alts)
= Case (Var v) x t <$> mapM (goAltR v) alts
go (Case e x t alts)
= Case e x t <$> mapM goAlt alts
go (App e1 e2)
= App <$> go e1 <*> go e2
go (Lam x e)
= Lam x <$> go e
go (Cast e c)
= (`Cast` c) <$> go e
go e
= return e
goRec (x, e)
= (x,) <$> go e
goAlt (Alt c bs e)
= Alt c bs <$> go e
goAltR v (Alt c bs e)
= do m <- get
case L.lookup (c,v) m of
Just bs' -> return (Alt c bs' (substTuple bs' bs e))
Nothing -> do let bs' = mkAlive <$> bs
modify (((c,v),bs'):)
return (Alt c bs' e)
normalizeTuples :: CoreBind -> CoreBind
normalizeTuples cb
| NonRec x e <- cb
= NonRec x $ go e
| Rec xes <- cb
= let (xs,es) = unzip xes in
Rec $ zip xs (go <$> es)
where
go (Let (NonRec x ex) e)
| Case _ _ _ alts <- unTickExpr ex
, [Alt _ vs (Var z)] <- alts
, z `elem` vs
= Let (NonRec z (go ex)) (substTuple [z] [x] (go e))
go (Let (NonRec x ex) e)
= Let (NonRec x (go ex)) (go e)
go (Let (Rec xes) e)
= Let (Rec (mapSnd go <$> xes)) (go e)
go (App e1 e2)
= App (go e1) (go e2)
go (Lam x e)
= Lam x (go e)
go (Case e b t alt)
= Case (go e) b t ((\(Alt c bs e') -> Alt c bs (go e')) <$> alt)
go (Cast e c)
= Cast (go e) c
go (Tick t e)
= Tick t (go e)
go (Type t)
= Type t
go (Coercion c)
= Coercion c
go (Lit l)
= Lit l
go (Var x)
= Var x
--------------------------------------------------------------------------------
-- | A @RewriteRule@ is a function that maps a CoreExpr to another
--------------------------------------------------------------------------------
type RewriteRule = CoreExpr -> Maybe CoreExpr
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
rewriteBindWith :: RewriteRule -> CoreBind -> CoreBind
--------------------------------------------------------------------------------
rewriteBindWith r (NonRec x e) = NonRec x (rewriteWith r e)
rewriteBindWith r (Rec xes) = Rec (mapSnd (rewriteWith r) <$> xes)
--------------------------------------------------------------------------------
rewriteWith :: RewriteRule -> CoreExpr -> CoreExpr
--------------------------------------------------------------------------------
rewriteWith tx = go
where
go = txTop . step
txTop e = fromMaybe e (tx e)
goB (Rec xes) = Rec (mapSnd go <$> xes)
goB (NonRec x e) = NonRec x (go e)
step (Let b e) = Let (goB b) (go e)
step (App e e') = App (go e) (go e')
step (Lam x e) = Lam x (go e)
step (Cast e c) = Cast (go e) c
step (Tick t e) = Tick t (go e)
step (Case e x t cs) = Case (go e) x t ((\(Alt c bs e') -> Alt c bs (go e')) <$> cs)
step e@(Type _) = e
step e@(Lit _) = e
step e@(Var _) = e
step e@(Coercion _) = e
--------------------------------------------------------------------------------
-- | Rewriting Pattern-Match-Tuples --------------------------------------------
--------------------------------------------------------------------------------
{-
let CrazyPat x1 ... xn = e in e'
let t : (t1,...,tn) = "CrazyPat e ... (y1, ..., yn)"
xn = Proj t n
...
x1 = Proj t 1
in
e'
"crazy-pat"
-}
{- [NOTE] The following is the structure of a @PatMatchTup@
let x :: (t1,...,tn) = E[(x1,...,xn)]
yn = case x of (..., yn) -> yn
…
y1 = case x of (y1, ...) -> y1
in
E'
GOAL: simplify the above to:
E [ (x1,...,xn) := E' [y1 := x1,...,yn := xn] ]
TODO: several tests (e.g. tests/pos/zipper000.hs) fail because
the above changes the "type" the expression `E` and in "other branches"
the new type may be different than the old, e.g.
let (x::y::_) = e in
x + y
let t = case e of
h1::t1 -> case t1 of
(h2::t2) -> (h1, h2)
DEFAULT -> error @ (Int, Int)
DEFAULT -> error @ (Int, Int)
x = case t of (h1, _) -> h1
y = case t of (_, h2) -> h2
in
x + y
is rewritten to:
h1::t1 -> case t1 of
(h2::t2) -> h1 + h2
DEFAULT -> error @ (Int, Int)
DEFAULT -> error @ (Int, Int)
case e of
h1 :: h2 :: _ -> h1 + h2
DEFAULT -> error @ (Int, Int)
which, alas, is ill formed.
-}
--------------------------------------------------------------------------------
-- simplifyPatTuple :: RewriteRule
-- simplifyPatTuple e =
-- case simplifyPatTuple' e of
-- Just e' -> if Ghc.exprType e == Ghc.exprType e'
-- then Just e'
-- else Just (tracePpr ("YIKES: RWR " ++ showPpr e) e')
-- Nothing -> Nothing
_safeSimplifyPatTuple :: RewriteRule
_safeSimplifyPatTuple e
| Just e' <- simplifyPatTuple e
, Ghc.exprType e' == Ghc.exprType e
= Just e'
| otherwise
= Nothing
--------------------------------------------------------------------------------
simplifyPatTuple :: RewriteRule
--------------------------------------------------------------------------------
_tidyAlt :: Int -> Maybe CoreExpr -> Maybe CoreExpr
_tidyAlt n (Just (Let (NonRec cb expr) rest))
| Just (yes, e') <- takeBinds n rest
= Just $ Let (NonRec cb expr) $ foldl (\e (x, ex) -> Let (NonRec x ex) e) e' (reverse $ go $ reverse yes)
where
go xes@((_, e):_) = let bs = grapBinds e in mapSnd (replaceBinds bs) <$> xes
go [] = []
replaceBinds bs (Case c x t alt) = Case c x t (replaceBindsAlt bs <$> alt)
replaceBinds bs (Tick t e) = Tick t (replaceBinds bs e)
replaceBinds _ e = e
replaceBindsAlt bs (Alt c _ e) = Alt c bs e
grapBinds (Case _ _ _ alt) = grapBinds' alt
grapBinds (Tick _ e) = grapBinds e
grapBinds _ = []
grapBinds' [] = []
grapBinds' (Alt _ bs _ : _) = bs
_tidyAlt _ e
= e
simplifyPatTuple (Let (NonRec x e) rest)
| Just (n, ts ) <- varTuple x
, 2 <= n
, Just (yes, e') <- takeBinds n rest
, let ys = fst <$> yes
, Just _ <- hasTuple ys e
, matchTypes yes ts
= replaceTuple ys e e'
simplifyPatTuple _
= Nothing
varTuple :: Var -> Maybe (Int, [Type])
varTuple x
| TyConApp c ts <- Ghc.varType x
, isTupleTyCon c
= Just (length ts, ts)
| otherwise
= Nothing
takeBinds :: Nat -> CoreExpr -> Maybe ([(Var, CoreExpr)], CoreExpr)
takeBinds nat ce
| nat < 2 = Nothing
| otherwise = {- mapFst reverse <$> -} go nat ce
where
go 0 e = Just ([], e)
go n (Let (NonRec x e) e') = do (xes, e'') <- go (n-1) e'
Just ((x,e) : xes, e'')
go _ _ = Nothing
matchTypes :: [(Var, CoreExpr)] -> [Type] -> Bool
matchTypes xes ts = xN == tN
&& all (uncurry eqType) (safeZipWithError msg xts ts)
&& all isProjection es
where
xN = length xes
tN = length ts
xts = Ghc.varType <$> xs
(xs, es) = unzip xes
msg = "RW:matchTypes"
isProjection :: CoreExpr -> Bool
isProjection e = case lift e of
Just PatProject{} -> True
_ -> False
--------------------------------------------------------------------------------
-- | `hasTuple ys e` CHECKS if `e` contains a tuple that "looks like" (y1...yn)
--------------------------------------------------------------------------------
hasTuple :: [Var] -> CoreExpr -> Maybe [Var]
--------------------------------------------------------------------------------
hasTuple ys = stepE
where
stepE e
| Just xs <- isVarTup ys e = Just xs
| otherwise = go e
stepA (Alt DEFAULT _ _) = Nothing
stepA (Alt _ _ e) = stepE e
go (Let _ e) = stepE e
go (Case _ _ _ cs) = msum (stepA <$> cs)
go _ = Nothing
--------------------------------------------------------------------------------
-- | `replaceTuple ys e e'` REPLACES tuples that "looks like" (y1...yn) with e'
--------------------------------------------------------------------------------
replaceTuple :: [Var] -> CoreExpr -> CoreExpr -> Maybe CoreExpr
replaceTuple ys ce ce' = stepE ce
where
t' = Ghc.exprType ce'
stepE e
| Just xs <- isVarTup ys e = Just $ substTuple xs ys ce'
| otherwise = go e
stepA (Alt DEFAULT xs err) = Just (Alt DEFAULT xs (replaceIrrefutPat t' err))
stepA (Alt c xs e) = Alt c xs <$> stepE e
go (Let b e) = Let b <$> stepE e
go (Case e x t cs) = fixCase e x t <$> mapM stepA cs
go _ = Nothing
_showExpr :: CoreExpr -> String
_showExpr = show'
where
show' (App e1 e2) = show' e1 ++ " " ++ show' e2
show' (Var x) = _showVar x
show' (Let (NonRec x ex) e) = "Let " ++ _showVar x ++ " = " ++ show' ex ++ "\nIN " ++ show' e
show' (Tick _ e) = show' e
show' (Case e x _ alt) = "Case " ++ _showVar x ++ " = " ++ show' e ++ " OF " ++ unlines (showAlt' <$> alt)
show' e = showPpr e
showAlt' (Alt c bs e) = showPpr c ++ unwords (_showVar <$> bs) ++ " -> " ++ show' e
_showVar :: Var -> String
_showVar = show . F.symbol
_errorSkip :: String -> a -> b
_errorSkip x _ = error x
-- replaceTuple :: [Var] -> CoreExpr -> CoreExpr -> Maybe CoreExpr
-- replaceTuple ys e e' = tracePpr msg (_replaceTuple ys e e')
-- where
-- msg = "replaceTuple: ys = " ++ showPpr ys ++
-- " e = " ++ showPpr e ++
-- " e' =" ++ showPpr e'
-- | The substitution (`substTuple`) can change the type of the overall
-- case-expression, so we must update the type of each `Case` with its
-- new, possibly updated type. See:
-- https://github.com/ucsd-progsys/liquidhaskell/pull/752#issuecomment-228946210
fixCase :: CoreExpr -> Var -> Type -> ListNE (Alt Var) -> CoreExpr
fixCase e x _t cs' = Case e x t' cs'
where
t' = Ghc.exprType body
Alt _ _ body = c
c:_ = cs'
{-@ type ListNE a = {v:[a] | len v > 0} @-}
type ListNE a = [a]
replaceIrrefutPat :: Type -> CoreExpr -> CoreExpr
replaceIrrefutPat t (App (Lam z e) eVoid)
| Just e' <- replaceIrrefutPat' t e
= App (Lam z e') eVoid
replaceIrrefutPat t e
| Just e' <- replaceIrrefutPat' t e
= e'
replaceIrrefutPat _ e
= e
replaceIrrefutPat' :: Type -> CoreExpr -> Maybe CoreExpr
replaceIrrefutPat' t e
| (Var x, rep:_:args) <- collectArgs e
, isIrrefutErrorVar x
= Just (Ghc.mkCoreApps (Var x) (rep : Type t : args))
| otherwise
= Nothing
isIrrefutErrorVar :: Var -> Bool
-- isIrrefutErrorVar _x = False -- Ghc.iRREFUT_PAT_ERROR_ID == x -- TODO:GHC-863
isIrrefutErrorVar x = x == Ghc.pAT_ERROR_ID
--------------------------------------------------------------------------------
-- | `substTuple xs ys e'` returns e' [y1 := x1,...,yn := xn]
--------------------------------------------------------------------------------
substTuple :: [Var] -> [Var] -> CoreExpr -> CoreExpr
substTuple xs ys = substExpr (M.fromList $ zip ys xs)
--------------------------------------------------------------------------------
-- | `isVarTup xs e` returns `Just ys` if e == (y1, ... , yn) and xi ~ yi
--------------------------------------------------------------------------------
isVarTup :: [Var] -> CoreExpr -> Maybe [Var]
isVarTup xs e
| Just ys <- isTuple e
, eqVars xs ys = Just ys
isVarTup _ _ = Nothing
eqVars :: [Var] -> [Var] -> Bool
eqVars xs ys = {- F.tracepp ("eqVars: " ++ show xs' ++ show ys') -} xs' == ys'
where
xs' = {- F.symbol -} show <$> xs
ys' = {- F.symbol -} show <$> ys
isTuple :: CoreExpr -> Maybe [Var]
isTuple e
| (Var t, es) <- collectArgs e
, isTupleId t
, Just xs <- mapM isVar (secondHalf es)
= Just xs
| otherwise
= Nothing
isVar :: CoreExpr -> Maybe Var
isVar (Var x) = Just x
isVar _ = Nothing
secondHalf :: [a] -> [a]
secondHalf xs = drop (n `div` 2) xs
where
n = length xs