|
| 1 | +module Gibbon.L0.ElimNewtype where |
| 2 | + |
| 3 | +import Gibbon.L0.Syntax |
| 4 | +import Gibbon.Common |
| 5 | + |
| 6 | +import Control.Arrow |
| 7 | +import qualified Data.Map as M |
| 8 | +import qualified Data.Set as S |
| 9 | +import Data.Symbol ( unintern ) |
| 10 | + |
| 11 | +elimNewtypes :: Monad m => Prog0 -> m Prog0 |
| 12 | +elimNewtypes = pure . elimProgram |
| 13 | + |
| 14 | +packedOccurs :: Var -> Ty0 -> Bool |
| 15 | +packedOccurs v@(Var s) t = case t of |
| 16 | + PackedTy u ts |
| 17 | + | unintern s == u -> True |
| 18 | + | otherwise -> any go ts |
| 19 | + ProdTy ts -> any go ts |
| 20 | + SymDictTy _ x -> go x |
| 21 | + ArrowTy ts x -> any go ts || go x |
| 22 | + VectorTy x -> go x |
| 23 | + ListTy x -> go x |
| 24 | + _ -> False |
| 25 | + where |
| 26 | + go = packedOccurs v |
| 27 | + |
| 28 | +type TyMap = M.Map String ([Ty0] -> Ty0) |
| 29 | +-- type params -> type in terms of params -> args -> substituted type |
| 30 | +mkPolyNames :: [TyVar] -> Ty0 -> [Ty0] -> Ty0 |
| 31 | +mkPolyNames params paramty args = |
| 32 | + substTyVar (M.fromList $ zip params args) paramty |
| 33 | + |
| 34 | +elimProgram :: Prog0 -> Prog0 |
| 35 | +elimProgram prog = |
| 36 | + Prog |
| 37 | + { mainExp = (elimE connames tynames (ddefs prog) *** elimTy tynames) <$> mainExp prog |
| 38 | + , fundefs = fdefs |
| 39 | + , ddefs = tys |
| 40 | + } |
| 41 | + where |
| 42 | + (newtys, tys) = M.partition (\x -> case dataCons x of |
| 43 | + [(_, [(_, t)])] -> not $ packedOccurs (tyName x) t |
| 44 | + _ -> False |
| 45 | + ) (ddefs prog) |
| 46 | + tynames = |
| 47 | + M.mapKeys (\(Var x) -> unintern x) |
| 48 | + $ M.map (mkPolyNames . tyArgs <*> snd . head . snd . head . dataCons) newtys |
| 49 | + connames = S.fromList $ fst . head . dataCons <$> M.elems newtys |
| 50 | + fdefs = M.map (\d -> d { funTy=elimTyScheme tynames (funTy d) |
| 51 | + , funBody=elimE connames tynames (ddefs prog) (funBody d) |
| 52 | + }) (fundefs prog) |
| 53 | + |
| 54 | +elimE :: S.Set String -> TyMap -> DDefs Ty0 -> Exp0 -> Exp0 |
| 55 | +elimE cns tns dds e0 = case e0 of |
| 56 | + DataConE _ty0 s [e] |
| 57 | + | S.member s cns -> f e |
| 58 | + DataConE _ty0 s es -> DataConE _ty0 s (f <$> es) |
| 59 | + VarE _ -> e0 |
| 60 | + LitE _ -> e0 |
| 61 | + CharE _ -> e0 |
| 62 | + FloatE _ -> e0 |
| 63 | + LitSymE _ -> e0 |
| 64 | + AppE var ty es -> AppE var ty (f <$> es) |
| 65 | + PrimAppE p es -> PrimAppE (elimPrim tns p) (f <$> es) |
| 66 | + LetE (var, u, t, e1) e2 -> LetE (var, g <$> u, g t, f e1) (f e2) |
| 67 | + IfE e1 e2 e3 -> IfE (f e1) (f e2) (f e3) |
| 68 | + MkProdE es -> MkProdE (f <$> es) |
| 69 | + ProjE n e -> ProjE n (f e) |
| 70 | + CaseE e1 [(s, [(var, t)], e2)] |
| 71 | + | S.member s cns -> LetE (var, [], g t, f e1) (f e2) |
| 72 | + CaseE e x -> CaseE (f e) ((\(c, v, e1) -> (c, v, f e1)) <$> x) |
| 73 | + TimeIt e t b -> TimeIt (f e) (g t) b |
| 74 | + WithArenaE var e -> WithArenaE var (f e) |
| 75 | + SpawnE var ts es -> SpawnE var ts (f <$> es) |
| 76 | + |
| 77 | + Ext ext -> Ext (elimExt cns tns dds ext) |
| 78 | + _ -> e0 |
| 79 | + where |
| 80 | + f = elimE cns tns dds |
| 81 | + g = elimTy tns |
| 82 | + |
| 83 | +elimExt :: S.Set String -> TyMap -> DDefs Ty0 -> E0Ext Ty0 Ty0 -> E0Ext Ty0 Ty0 |
| 84 | +elimExt cns tns dds ext0 = case ext0 of |
| 85 | + LambdaE args applicand -> LambdaE (second g <$> args) (f applicand) |
| 86 | + FunRefE locs var -> FunRefE (g <$> locs) var |
| 87 | + PolyAppE pe1 pe2 -> PolyAppE (f pe1) (f pe2) |
| 88 | + BenchE var locs preexps bool -> BenchE var (g <$> locs) (f <$> preexps) bool |
| 89 | + ParE0 preexps -> ParE0 (f <$> preexps) |
| 90 | + PrintPacked dec preexp -> PrintPacked (g dec) (f preexp) |
| 91 | + CopyPacked dec preexp -> CopyPacked (g dec) (f preexp) |
| 92 | + TravPacked dec preexp -> TravPacked (g dec) (f preexp) |
| 93 | + L loc preexp -> L loc (f preexp) |
| 94 | + LinearExt (ReverseAppE pe1 pe2) -> LinearExt (ReverseAppE (f pe1) (f pe2)) |
| 95 | + LinearExt (LseqE pe1 pe2) -> LinearExt (LseqE (f pe1) (f pe2)) |
| 96 | + LinearExt (AliasE pe) -> LinearExt (AliasE (f pe)) |
| 97 | + LinearExt (ToLinearE pe) -> LinearExt (ToLinearE (f pe)) |
| 98 | + where |
| 99 | + f = elimE cns tns dds |
| 100 | + g = elimTy tns |
| 101 | + |
| 102 | +elimPrim :: TyMap -> Prim Ty0 -> Prim Ty0 |
| 103 | +elimPrim tns p0 = case p0 of |
| 104 | + ErrorP s t -> ErrorP s (f t) |
| 105 | + DictInsertP t -> DictInsertP (f t) |
| 106 | + DictLookupP t -> DictLookupP (f t) |
| 107 | + DictEmptyP t -> DictEmptyP (f t) |
| 108 | + DictHasKeyP t -> DictHasKeyP (f t) |
| 109 | + PDictAllocP t1 t2 -> PDictAllocP (f t1) (f t2) |
| 110 | + PDictInsertP t1 t2 -> PDictInsertP (f t1) (f t2) |
| 111 | + PDictLookupP t1 t2 -> PDictLookupP (f t1) (f t2) |
| 112 | + PDictHasKeyP t1 t2 -> PDictHasKeyP (f t1) (f t2) |
| 113 | + PDictForkP t1 t2 -> PDictForkP (f t1) (f t2) |
| 114 | + PDictJoinP t1 t2 -> PDictJoinP (f t1) (f t2) |
| 115 | + LLAllocP t -> LLAllocP (f t) |
| 116 | + LLIsEmptyP t -> LLIsEmptyP (f t) |
| 117 | + LLConsP t -> LLConsP (f t) |
| 118 | + LLHeadP t -> LLHeadP (f t) |
| 119 | + LLTailP t -> LLTailP (f t) |
| 120 | + LLFreeP t -> LLFreeP (f t) |
| 121 | + LLCopyP t -> LLCopyP (f t) |
| 122 | + VAllocP t -> VAllocP (f t) |
| 123 | + VFreeP t -> VFreeP (f t) |
| 124 | + VFree2P t -> VFree2P (f t) |
| 125 | + VLengthP t -> VLengthP (f t) |
| 126 | + VNthP t -> VNthP (f t) |
| 127 | + VSliceP t -> VSliceP (f t) |
| 128 | + InplaceVUpdateP t -> InplaceVUpdateP (f t) |
| 129 | + VConcatP t -> VConcatP (f t) |
| 130 | + VSortP t -> VSortP (f t) |
| 131 | + InplaceVSortP t -> InplaceVSortP (f t) |
| 132 | + VMergeP t -> VMergeP (f t) |
| 133 | + ReadPackedFile ms s mVar t -> ReadPackedFile ms s mVar (f t) |
| 134 | + WritePackedFile s t -> WritePackedFile s (f t) |
| 135 | + ReadArrayFile m t -> ReadArrayFile m (f t) |
| 136 | + _ -> p0 |
| 137 | + where |
| 138 | + f = elimTy tns |
| 139 | + |
| 140 | +elimTyScheme :: TyMap -> TyScheme -> TyScheme |
| 141 | +elimTyScheme tns (ForAll tvs t) = ForAll tvs (elimTy tns t) |
| 142 | + |
| 143 | +elimTy :: TyMap -> Ty0 -> Ty0 |
| 144 | +elimTy tns t0 = case t0 of |
| 145 | + PackedTy s args -> |
| 146 | + maybe (PackedTy s (f <$> args)) (f . ($ f <$> args)) (M.lookup s tns) |
| 147 | + ProdTy ts -> ProdTy (f <$> ts) |
| 148 | + SymDictTy varMaybe t -> SymDictTy varMaybe (f t) |
| 149 | + ArrowTy ts t -> ArrowTy (f <$> ts) (f t) |
| 150 | + VectorTy t -> VectorTy (f t) |
| 151 | + PDictTy tK tV -> PDictTy (f tK) (f tV) |
| 152 | + ListTy t -> ListTy (f t) |
| 153 | + _ -> t0 |
| 154 | + where |
| 155 | + f = elimTy tns |
0 commit comments