@@ -8,7 +8,7 @@ Maintainer : The Idris Community.
9{-# LANGUAGE PatternGuards #-}
9{-# LANGUAGE PatternGuards #-}
10{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
10{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
11−module Idris.Core.WHNF(whnf, WEnv) where
11+module Idris.Core.WHNF(whnf, whnfArgs, WEnv) where
13import Idris.Core.CaseTree
13import Idris.Core.CaseTree
14import Idris.Core.Evaluate hiding (quote)
14import Idris.Core.Evaluate hiding (quote)
@@ -37,7 +37,8 @@ type Stack = [StackEntry]
37data WHNF = WDCon Int Int Bool Name (Term, WEnv) -- ^ data constructor
37data WHNF = WDCon Int Int Bool Name (Term, WEnv) -- ^ data constructor
38 | WTCon Int Int Name (Type, WEnv) -- ^ type constructor
38 | WTCon Int Int Name (Type, WEnv) -- ^ type constructor
39 | WPRef Name (Term, WEnv) -- ^irreducible global (e.g. a postulate)
39 | WPRef Name (Term, WEnv) -- ^irreducible global (e.g. a postulate)
40− | WBind Name (Binder Term) (Term, WEnv)
41+ | WBind Name (Binder WHNF) (Term, WEnv)
41 | WApp WHNF (Term, WEnv)
42 | WApp WHNF (Term, WEnv)
42 | WConstant Const
43 | WConstant Const
43 | WProj WHNF Int
44 | WProj WHNF Int
@@ -46,7 +47,7 @@ data WHNF = WDCon Int Int Bool Name (Term, WEnv) -- ^ data constructor
47 | WImpossible
48 | WImpossible
49−-- | Reduce a *closed* term to weak head normal form.
50+-- | Reduce a term to weak head normal form.
50whnf :: Context -> Env -> Term -> Term
51whnf :: Context -> Env -> Term -> Term
51whnf ctxt env tm =
52whnf ctxt env tm =
52 inlineSmall ctxt env $ -- reduce small things in body. This is primarily
53 inlineSmall ctxt env $ -- reduce small things in body. This is primarily
@@ -55,6 +56,14 @@ whnf ctxt env tm =
55 -- easier to read.
56 -- easier to read.
56 quote (do_whnf ctxt (map finalEntry env) tm)
57 quote (do_whnf ctxt (map finalEntry env) tm)
59+-- | Reduce a type so that all arguments are expanded, and all argument types
60+-- are in weak head normal form
61+whnfArgs :: Context -> Env -> Term -> Term
63+ = case whnf ctxt env tm of
64+ Bind n b@(Pi _ ty _) sc -> Bind n b (whnfArgs ctxt ((n,b):env) sc)
58finalEntry :: (Name, Binder (TT Name)) -> (Name, Binder (TT Name))
67finalEntry :: (Name, Binder (TT Name)) -> (Name, Binder (TT Name))
59finalEntry (n, b) = (n, fmap finalise b)
68finalEntry (n, b) = (n, fmap finalise b)
@@ -65,12 +74,13 @@ do_whnf ctxt genv tm = eval (WEnv 0 []) [] tm
65 eval wenv@(WEnv d env) stk (V i)
74 eval wenv@(WEnv d env) stk (V i)
66 | i < length env = let (tm, env') = env !! i in
75 | i < length env = let (tm, env') = env !! i in
67 eval env' stk tm
76 eval env' stk tm
68− | otherwise = error "Can't happen: WHNF scope error"
77+ | otherwise = WV i
69 eval wenv@(WEnv d env) stk (Bind n (Let t v) sc)
78 eval wenv@(WEnv d env) stk (Bind n (Let t v) sc)
70 = eval (WEnv d ((v, wenv) : env)) stk sc
79 = eval (WEnv d ((v, wenv) : env)) stk sc
71− eval (WEnv d env) [] (Bind n b sc) = WBind n b (sc, WEnv (d + 1) env)
72− eval (WEnv d env) ((tm, tenv) : stk) (Bind n b sc)
80+ eval (WEnv d env) ((tm, tenv) : stk) (Bind n b@(Lam _) sc)
73 = eval (WEnv d ((tm, tenv) : env)) stk sc
81 = eval (WEnv d ((tm, tenv) : env)) stk sc
82+ eval wenv@(WEnv d env) stk (Bind n b sc) -- stk must be empty if well typed
83+ = WBind n (fmap (eval wenv []) b) (sc, WEnv (d + 1) env)
75 eval env stk (P nt n ty)
85 eval env stk (P nt n ty)
76 | Just (Let t v) <- lookup n genv = eval env stk v
86 | Just (Let t v) <- lookup n genv = eval env stk v
@@ -204,7 +214,8 @@ quote :: WHNF -> Term
204quote (WDCon t a u n (ty, env)) = P (DCon t a u) n (quoteEnv env ty)
214quote (WDCon t a u n (ty, env)) = P (DCon t a u) n (quoteEnv env ty)
205quote (WTCon t a n (ty, env)) = P (TCon t a) n (quoteEnv env ty)
215quote (WTCon t a n (ty, env)) = P (TCon t a) n (quoteEnv env ty)
206quote (WPRef n (ty, env)) = P Ref n (quoteEnv env ty)
216quote (WPRef n (ty, env)) = P Ref n (quoteEnv env ty)
207−quote (WBind n ty (sc, env)) = Bind n ty (quoteEnv env sc)
218+quote (WBind n b (sc, env)) = Bind n (fmap quote b) (quoteEnv env sc)
208quote (WApp f (a, env)) = App Complete (quote f) (quoteEnv env a)
219quote (WApp f (a, env)) = App Complete (quote f) (quoteEnv env a)
209quote (WConstant c) = Constant c
220quote (WConstant c) = Constant c
210quote (WProj t i) = Proj (quote t) i
221quote (WProj t i) = Proj (quote t) i