@@ -118,7 +118,9 @@ simplify ctxt runtime env t
118118
119119hnf :: Context -> Env -> TT Name -> TT Name
120120hnf ctxt env t
121− = evalState (do val <- eval False ctxt threshold [] (map finalEntry env) (finalise t) [HNF]
121+ = evalState (do val <- eval False ctxt threshold []
122+ (map finalEntry env)
123+ (finalise t) [HNF]
122124 quote 0 val) initEval
123125
124126
@@ -159,6 +161,7 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
159161 spec = Spec `elem` opts
160162 simpl = Simplify True `elem` opts
161163 atRepl = AtREPL `elem` opts
164+ hnf = HNF `elem` opts
162165
163166 canSimplify inl inr n stk
164167 = (Simplify False `elem` opts && (not inl || elem n stk))
@@ -168,6 +171,7 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
168171 | Just (Let t v) <- lookup n genv = do when (not atRepl) $ step maxred
169172 ev ntimes stk top env v
170173 ev ntimes_in stk top env (P Ref n ty)
174+ | not top && hnf = liftM (VP Ref n) (ev ntimes stk top env ty)
171175 | (True, ntimes) <- usable simpl n ntimes_in
172176 = do let val = lookupDefAcc Nothing n atRepl ctxt
173177 when (not atRepl) $ step maxred
@@ -211,7 +215,7 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
211215 ev ntimes stk top env (Bind n b sc)
212216 = do b' <- vbind env b
213217 when (not atRepl) $ step maxred
214− return $ VBind n b' (\x -> ev ntimes stk top (x:env) sc)
218+ return $ VBind n b' (\x -> ev ntimes stk False (x:env) sc)
215219 where vbind env t
216220 | simpl
217221 = fmapMB (\tm -> ev ((MN 0 "STOP", 0) : ntimes)
@@ -222,7 +226,7 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
222226-- | laz == UN "lazy"
223227-- = trace (showEnvDbg genv a) $ ev ntimes stk top env a
224228 ev ntimes stk top env (App f a)
225− = do f' <- ev ntimes stk top env f
229+ = do f' <- ev ntimes stk False env f
226230 a' <- ev ntimes stk False env a
227231 when (not atRepl) $ step maxred
228232 evApply ntimes stk top env [a'] f'
@@ -243,7 +247,10 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
243247 wknV (-1) app
244248-- apply ntimes stk False env f args
245249-- | spec = specApply ntimes stk env f args
246− apply ntimes_in stk top env f@(VP Ref n ty) args
250+ apply ntimes_in stk top env f@(VP Ref n ty) args
251+ | not top && hnf = case args of
252+ [] -> return f
253+ _ -> return $ unload env f args
247254 | (True, ntimes) <- usable simpl n ntimes_in
248255 = traceWhen traceon (show stk) $
249256 do let val = lookupDefAcc Nothing n atRepl ctxt
@@ -284,7 +291,8 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
284291 | length ns <= length args
285292 = do let args' = take (length ns) args
286293 let rest = drop (length ns) args
287− t <- evTree ntimes stk top env (zipWith (\n t -> (n, t)) ns args') tree
294+ t <- evTree ntimes stk top env
295+ (zipWith (\n t -> (n, t)) ns args') tree
288296 return (t, rest)
289297 | otherwise = return (Nothing, args)
290298
@@ -293,7 +301,8 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
293301 evTree ntimes stk top env amap (UnmatchedCase str) = return Nothing
294302 evTree ntimes stk top env amap (STerm tm)
295303 = do let etm = pToVs (map fst amap) tm
296− etm' <- ev ntimes stk top (map snd amap ++ env) etm
304+ etm' <- ev ntimes stk (not (conHeaded tm))
305+ (map snd amap ++ env) etm
297306 return $ Just etm'
298307 evTree ntimes stk top env amap (Case n alts)
299308 = case lookup n amap of
@@ -306,6 +315,10 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
306315 _ -> return Nothing
307316 _ -> return Nothing
308317
318+ conHeaded tm@(App _ _)
319+ | (P (DCon _ _) _ _, args) <- unApply tm = True
320+ conHeaded t = False
321+
309322 chooseAlt' ntimes stk env _ (f, args) alts amap
310323 = do f' <- apply ntimes stk True env f args
311324 chooseAlt env f' (getValArgs f') alts amap