|
| 1 | +module LambdaComp.CBPV.Optimization.BetaReduction |
| 2 | + ( runBetaReduction |
| 3 | + ) where |
| 4 | + |
| 5 | +import LambdaComp.CBPV.Syntax |
| 6 | + |
| 7 | +runBetaReduction :: Tm Com -> Tm Com |
| 8 | +runBetaReduction = betaReduction |
| 9 | + |
| 10 | +betaReduction :: Tm c -> Tm c |
| 11 | +betaReduction tm@(TmVar _) = tm |
| 12 | +betaReduction tm@(TmGlobal _) = tm |
| 13 | +betaReduction tm@TmUnit = tm |
| 14 | +betaReduction tm@TmTrue = tm |
| 15 | +betaReduction tm@TmFalse = tm |
| 16 | +betaReduction tm@(TmInt _) = tm |
| 17 | +betaReduction tm@(TmDouble _) = tm |
| 18 | +betaReduction (TmThunk tm) = TmThunk $ betaReduction tm |
| 19 | +betaReduction (TmIf tm0 tm1 tm2) = |
| 20 | + case betaReduction tm0 of |
| 21 | + TmTrue -> tm1' |
| 22 | + TmFalse -> tm2' |
| 23 | + tm0' -> TmIf tm0' tm1' tm2' |
| 24 | + where |
| 25 | + tm1' = betaReduction tm1 |
| 26 | + tm2' = betaReduction tm2 |
| 27 | +betaReduction (TmLam p tm) = TmLam p $ betaReduction tm |
| 28 | +betaReduction (tmf `TmApp` tma) = |
| 29 | + case betaReduction tmf of |
| 30 | + TmLam Param{..} tmf' -> TmLet paramName (betaReduction tma) tmf' |
| 31 | + tmf' -> tmf' `TmApp` betaReduction tma |
| 32 | +betaReduction (TmForce tm) = |
| 33 | + case betaReduction tm of |
| 34 | + TmThunk tm' -> tm' |
| 35 | + tm' -> TmForce tm' |
| 36 | +betaReduction (TmReturn tm) = TmReturn $ betaReduction tm |
| 37 | +betaReduction (TmTo tm0 x tm1) = |
| 38 | + case betaReduction tm0 of |
| 39 | + TmReturn tm0' -> TmLet x tm0' (betaReduction tm1) |
| 40 | + tm0' -> TmTo tm0' x (betaReduction tm1) |
| 41 | +betaReduction (TmLet x tm0 tm1) = TmLet x (betaReduction tm0) (betaReduction tm1) |
| 42 | +betaReduction (TmPrimBinOp op tm0 tm1) = TmPrimBinOp op (betaReduction tm0) (betaReduction tm1) |
| 43 | +betaReduction (TmPrimUnOp op tm) = TmPrimUnOp op $ betaReduction tm |
| 44 | +betaReduction (TmPrintInt tm0 tm1) = TmPrintInt (betaReduction tm0) (betaReduction tm1) |
| 45 | +betaReduction (TmPrintDouble tm0 tm1) = TmPrintDouble (betaReduction tm0) (betaReduction tm1) |
| 46 | +betaReduction (TmRec p tm) = TmRec p (betaReduction tm) |
0 commit comments