@@ -251,6 +251,9 @@ execApp env ctxt c@(EP (TCon _ arity) n _) args =
251251 let restArgs = drop arity args
252252 execApp env ctxt (mkEApp c args') restArgs
253253
254+execApp env ctxt f@(EP _ n _) args
255+ | Just (res, rest) <- getOp n args = do r <- res
256+ execApp env ctxt r rest
254257execApp env ctxt f@(EP _ n _) args =
255258 do let val = lookupDef n ctxt
256259 case val of
@@ -260,9 +263,9 @@ execApp env ctxt f@(EP _ n _) args =
260263 if length args >= arity
261264 then let args' = take arity args in
262265 case getOp n args' of
263− Just res -> do r <- res
264− execApp env ctxt r (drop arity args)
265− Nothing -> return (mkEApp f args)
266+ Just (res, []) -> do r <- res
267+ execApp env ctxt r (drop arity args)
268+ _ -> return (mkEApp f args)
266269 else return (mkEApp f args)
267270 [CaseOp _ _ _ _ _ (CaseDefs _ ([], STerm tm) _ _)] -> -- nullary fun
268271 do rhs <- doExec env ctxt tm
@@ -418,41 +421,47 @@ upio = sUN "unsafePerformPrimIO"
418421delay = sUN "Delay"
419422force = sUN "Force"
420423
421−-- | Look up primitive operations in the global table and transform them into ExecVal functions
422−getOp :: Name -> [ExecVal] -> Maybe (Exec ExecVal)
423−getOp fn [_, _, x] | fn == pbm = Just (return x)
424−getOp fn [_, EConstant (Str n)]
424+-- | Look up primitive operations in the global table and transform
425+-- them into ExecVal functions
426+getOp :: Name -> [ExecVal] -> Maybe (Exec ExecVal, [ExecVal])
427+getOp fn (_ : _ : x : xs) | fn == pbm = Just (return x, xs)
428+getOp fn (_ : EConstant (Str n) : xs)
425429 | fn == pws =
426− Just $ do execIO $ putStr n
427− return (EConstant (I 0))
428−getOp fn [_]
430+ Just (do execIO $ putStr n
431+ return (EConstant (I 0)), xs)
432+getOp fn (_:xs)
429433 | fn == prs =
430− Just $ do line <- execIO getLine
431− return (EConstant (Str line))
432−getOp fn [_, EP _ fn' _, EConstant (Str n)]
434+ Just (do line <- execIO getLine
435+ return (EConstant (Str line)), xs)
436+getOp fn (_ : EP _ fn' _ : EConstant (Str n) : xs)
433437 | fn == pwf && fn' == pstdout =
434− Just $ do execIO $ putStr n
435− return (EConstant (I 0))
436−getOp fn [_, EP _ fn' _]
438+ Just (do execIO $ putStr n
439+ return (EConstant (I 0)), xs)
440+getOp fn (_ : EP _ fn' _ : xs)
437441 | fn == prf && fn' == pstdin =
438− Just $ do line <- execIO getLine
439− return (EConstant (Str line))
440−getOp fn [_, EHandle h, EConstant (Str n)]
442+ Just (do line <- execIO getLine
443+ return (EConstant (Str line)), xs)
444+getOp fn (_ : EHandle h : EConstant (Str n) : xs)
441445 | fn == pwf =
442− Just $ do execIO $ hPutStr h n
443− return (EConstant (I 0))
444−getOp fn [_, EHandle h]
446+ Just (do execIO $ hPutStr h n
447+ return (EConstant (I 0)), xs)
448+getOp fn (_ : EHandle h : xs)
445449 | fn == prf =
446− Just $ do contents <- execIO $ hGetLine h
447− return (EConstant (Str (contents ++ "\n")))
448−getOp fn [_, arg]
450+ Just (do contents <- execIO $ hGetLine h
451+ return (EConstant (Str (contents ++ "\n"))), xs)
452+getOp fn (_ : arg : xs)
449453 | fn == prf =
450− Just $ execFail (Msg "Can't use prim__readFile on a raw pointer in the executor.")
451−getOp n args = getPrim n primitives >>= flip applyPrim args
452− where getPrim :: Name -> [Prim] -> Maybe ([ExecVal] -> Maybe ExecVal)
454+ Just $ (execFail (Msg "Can't use prim__readFile on a raw pointer in the executor."), xs)
455+getOp n args = do (arity, prim) <- getPrim n primitives
456+ if (length args >= arity)
457+ then do res <- applyPrim prim (take arity args)
458+ Just (res, drop arity args)
459+ else Nothing
460+ where getPrim :: Name -> [Prim] -> Maybe (Int, [ExecVal] -> Maybe ExecVal)
453461 getPrim n [] = Nothing
454− getPrim n ((Prim pn _ arity def _ _) : prims) | n == pn = Just $ execPrim def
455− | otherwise = getPrim n prims
462+ getPrim n ((Prim pn _ arity def _ _) : prims)
463+ | n == pn = Just (arity, execPrim def)
464+ | otherwise = getPrim n prims
456465
457466 execPrim :: ([Const] -> Maybe Const) -> [ExecVal] -> Maybe ExecVal
458467 execPrim f args = EConstant <$> (mapM getConst args >>= f)