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