@@ -29,6 +29,7 @@ import Idris.Core.CaseTree
30import Control.Applicative
30import Control.Applicative
31import Control.Monad.State
31import Control.Monad.State
32import Data.List
33import Data.List
33import Data.IntSet (IntSet)
34import Data.IntSet (IntSet)
34import qualified Data.IntMap as IM
35import qualified Data.IntMap as IM
@@ -267,34 +268,42 @@ instance ToIR (TT Name) where
267 = irCon env t a n args
268 = irCon env t a n args
268 | (P (TCon t a) n _, args) <- unApply tm
269 | (P (TCon t a) n _, args) <- unApply tm
269 = return LNothing
270 = return LNothing
270− | (P _ n _, args) <- unApply tm
272+ | (P _ n _, args) <- unApply tm = do
271− = do i <- getIState
273+ ist <- getIState
274+ case lookup n (idris_scprims ist) of
275+ -- if it's a primitive, compile to the corresponding code
272− args' <- mapM (ir' env) args
276+ Just _ -> fromJust . getPrim ist n <$> mapM (ir' env) args
273− case getPrim i n args' of
277+
274− Just tm -> return tm
277− = case lookupCtxtExact n (idris_optimisation i) of
278− Just oi -> collapsible oi
281− = case lookupCtxtExact n (idris_callgraph i) of
282− Just (CGInfo _ _ _ _ usedpos) -> Just usedpos
283− _ -> Nothing
278+ Nothing
284− if collapse
279+ -- if it's collapsible, compile to LNothing
285− then return LNothing
280+ | (collapsible <$> lookupCtxtExact n (idris_optimisation ist)) == Just True
286− else do
281+ -> return LNothing
287− iLOG $ "ERASE:\n " ++ show tm ++ "\n ==>\n " ++ show (LApp False (LV (Glob n)) (mkUsed used 0 args'))
282+
288− return (LApp False (LV (Glob n))
283+ -- otherwise, compile normally
289− (mkUsed used 0 args'))
284+ | otherwise -> applyName n ist args
290 | (f, args) <- unApply tm
286 | (f, args) <- unApply tm
291 = do f' <- ir' env f
287 = do f' <- ir' env f
292 args' <- mapM (ir' env) args
288 args' <- mapM (ir' env) args
293 return (LApp False f' args')
289 return (LApp False f' args')
294− where mkUsed (Just u) i [] = []
291+ where
295− mkUsed (Just u) i (x : xs) | i `elem` u = x : mkUsed (Just u) (i + 1) xs
292+ applyName :: Name -> IState -> [Term] -> Idris LExp
296− | otherwise = LNothing : mkUsed (Just u) (i + 1) xs
293+ applyName n ist args =
297− mkUsed Nothing _ xs = xs
294+ LApp False (LV $ Glob n) <$> mapM (ir' env . erase) (zip [0..] args)
297+ | i >= arity || i `elem` used = x
298+ | otherwise = Erased
300+ arity = case fst4 <$> lookupCtxt n (definitions . tt_ctxt $ ist) of
301+ [CaseOp ci ty tys def tot cdefs] -> length tys
304+ used = fromMaybe [] (usedpos <$> lookupCtxtExact n (idris_callgraph ist))
298-- ir' env (P _ (NS (UN "Z") ["Nat", "Prelude"]) _)
307-- ir' env (P _ (NS (UN "Z") ["Nat", "Prelude"]) _)
299-- = return $ LConst (BI 0)
308-- = return $ LConst (BI 0)
300 ir' env (P _ n _) = return $ LV (Glob n)
309 ir' env (P _ n _) = return $ LV (Glob n)