@@ -24,17 +24,20 @@ import Foreign.Marshal.Alloc (free)
2424-- IO was performed.
2525step :: TT Name -> Idris (Maybe (TT Name))
2626step tm = step' (unApply tm)
27− where step' (P _ (UN "unsafePerformIO") _, [_, arg] ) = return $ Just arg
27+ where step' (P _ (UN "unsafePerformIO") _, [_, arg] ) = step arg -- Only step if arg can be stepped
2828 step' (P _ (UN "mkForeign") _, args) = stepForeign args
2929 step' _ = return Nothing
3030
3131-- | Perform side effects until no more can be performed, then return the
3232-- resulting term (possibly the argument).
3333execute :: TT Name -> Idris (TT Name)
34−execute tm = do stepped <- step tm
35− case stepped of
36− Nothing -> return tm
37− Just tm' -> execute tm'
34+execute tm = case unApply tm of
35+ (P _ (UN "unsafePerformIO") _, [_, arg] ) -> execute' arg
36+ _ -> return tm
37+ where execute' tm = do stepped <- step tm
38+ case stepped of
39+ Nothing -> return tm
40+ Just tm' -> execute' tm'
3841
3942
4043
@@ -46,8 +49,7 @@ call :: Foreign -> [TT Name] -> Idris (Maybe (TT Name))
4649call (FFun name argTypes retType) args = do fn <- findForeign name
4750 case fn of
4851 Nothing -> return Nothing
49− Just f -> do res <- call' f args retType
50− return . Just $ res
52+ Just f -> return . Just =<< call' f args retType
5153 where call' :: ForeignFun -> [TT Name] -> FTy -> Idris (TT Name)
5254 call' (Fun _ h) args FInt = do res <- lift $ callFFI h retCInt (prepArgs args)
5355 return (Constant (I (fromIntegral res)))
@@ -65,7 +67,6 @@ call (FFun name argTypes retType) args = do fn <- findForeign name
6567 call' (Fun _ h) args FUnit = do res <- lift $ callFFI h retVoid (prepArgs args)
6668 return (P Ref unitCon (P Ref unitTy (TType (UVal 0)))) -- FIXME check universe level
6769
68−
6970 prepArgs = map prepArg
7071 prepArg (Constant (I i)) = argCInt (fromIntegral i)
7172 prepArg (Constant (Fl f)) = argCDouble (realToFrac f)