@@ -69,7 +69,7 @@ check ctxt env tm
6969 = evalStateT (check' True ctxt env tm) (0, []) -- Holes allowed
7070
7171check' :: Bool -> Context -> Env -> Raw -> StateT UCs TC (Term, Type)
72−check' holes ctxt env top = chk (TType (UVar (-5))) env top where
72+check' holes ctxt env top = chk (TType (UVar (-5))) Nothing env top where
7373
7474 smaller (UType NullType) _ = UType NullType
7575 smaller _ (UType NullType) = UType NullType
@@ -78,23 +78,47 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
7878 smaller x _ = x
7979
8080 chk :: Type -> -- uniqueness level
81+ Maybe UExp -> -- maximum universe level, if known
8182 Env -> Raw -> StateT UCs TC (Term, Type)
82− chk u env (Var n)
83+ chk u maxu env (Var n)
8384 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
8485 -- If we're elaborating, we don't want the private names; if we're
8586 -- checking an already elaborated term, we do
8687 | (P nt n' ty : _) <- lookupP_all (not holes) n ctxt
8788 = return (P nt n' ty, ty)
8889 | otherwise = do lift $ tfail $ NoSuchVariable n
89− chk u env ap@(RApp f a)
90− = do (fv, fty) <- chk u env f
91− (av, aty) <- chk u env a
90+ chk u maxu env ap@(RApp f RType) | not holes
91+ -- special case to reduce constraintss
92+ = do (fv, fty) <- chk u maxu env f
9293 let fty' = case uniqueBinders (map fst env) (finalise fty) of
9394 ty@(Bind x (Pi i s k) t) -> ty
9495 _ -> uniqueBinders (map fst env)
9596 $ case hnf ctxt env fty of
9697 ty@(Bind x (Pi i s k) t) -> ty
9798 _ -> normalise ctxt env fty
99+ case fty' of
100+ Bind x (Pi i (TType v') k) t ->
101+ do (v, cs) <- get
102+ put (v+1, ULT (UVar v) v' : cs)
103+ let apty = simplify initContext env
104+ (Bind x (Let (TType v') (TType (UVar v))) t)
105+ return (App fv (TType (UVar v)), apty)
106+ Bind x (Pi i s k) t ->
107+ do (av, aty) <- chk u maxu env RType
108+ convertsC ctxt env aty s
109+ let apty = simplify initContext env
110+ (Bind x (Let aty av) t)
111+ return (App fv av, apty)
112+ t -> lift $ tfail $ NonFunctionType fv fty
113+ chk u maxu env ap@(RApp f a)
114+ = do (fv, fty) <- chk u maxu env f
115+ let fty' = case uniqueBinders (map fst env) (finalise fty) of
116+ ty@(Bind x (Pi i s k) t) -> ty
117+ _ -> uniqueBinders (map fst env)
118+ $ case hnf ctxt env fty of
119+ ty@(Bind x (Pi i s k) t) -> ty
120+ _ -> normalise ctxt env fty
121+ (av, aty) <- chk u maxu env a
98122 case fty' of
99123 Bind x (Pi i s k) t ->
100124 do convertsC ctxt env aty s
@@ -102,13 +126,13 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
102126 (Bind x (Let aty av) t)
103127 return (App fv av, apty)
104128 t -> lift $ tfail $ NonFunctionType fv fty
105− chk u env RType
129+ chk u maxu env RType
106130 | holes = return (TType (UVal 0), TType (UVal 0))
107131 | otherwise = do (v, cs) <- get
108132 let c = ULT (UVar v) (UVar (v+1))
109133 put (v+2, (c:cs))
110134 return (TType (UVar v), TType (UVar (v+1)))
111− chk u env (RUType un)
135+ chk u maxu env (RUType un)
112136 | holes = return (UType un, TType (UVal 0))
113137 | otherwise = do -- TODO! Issue #1715 on the issue tracker.
114138 -- https://github.com/idris-lang/Idris-dev/issues/1715
@@ -117,8 +141,8 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
117141 -- put (v+2, (c:cs))
118142 -- return (TType (UVar v), TType (UVar (v+1)))
119143 return (UType un, TType (UVal 0))
120− chk u env (RConstant Forgot) = return (Erased, Erased)
121− chk u env (RConstant c) = return (Constant c, constType c)
144+ chk u maxu env (RConstant Forgot) = return (Erased, Erased)
145+ chk u maxu env (RConstant c) = return (Constant c, constType c)
122146 where constType (I _) = Constant (AType (ATInt ITNative))
123147 constType (BI _) = Constant (AType (ATInt ITBig))
124148 constType (Fl _) = Constant (AType ATFloat)
@@ -135,17 +159,25 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
135159 constType TheWorld = Constant WorldType
136160 constType Forgot = Erased
137161 constType _ = TType (UVal 0)
138− chk u env (RForce t) = do (_, ty) <- chk u env t
139− return (Erased, ty)
140− chk u env (RBind n (Pi i s k) t)
141− = do (sv, st) <- chk u env s
142− (kv, kt) <- chk u env k
143− (tv, tt) <- chk st ((n, Pi i sv kv) : env) t
162+ chk u maxu env (RForce t) = do (_, ty) <- chk u maxu env t
163+ return (Erased, ty)
164+ chk u maxu env (RBind n (Pi i s k) t)
165+ = do (sv, st) <- chk u Nothing env s
166+ vcs <- get
167+ (kv, kt) <- chk u Nothing env k
168+ put vcs -- no need to validate these constraints, they are independent
144169 (v, cs) <- get
170+ nmaxu <- case maxu of
171+ Nothing -> do put (v+1, cs)
172+ return (UVar v)
173+ Just m -> return m
174+ (tv, tt) <- chk st (Just nmaxu) ((n, Pi i sv kv) : env) t
145175 case (normalise ctxt env st, normalise ctxt env tt) of
146176 (TType su, TType tu) -> do
147− when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
148− let k' = st `smaller` kv `smaller` TType (UVar v) `smaller` u
177+ when (not holes) $ do (v, cs) <- get
178+ put (v, ULE su nmaxu :
179+ ULE tu nmaxu : cs)
180+ let k' = TType (UVar v) `smaller` st `smaller` kv `smaller` u
149181 return (Bind n (Pi i (uniqueBinders (map fst env) sv) k')
150182 (pToV n tv), k')
151183 (un, un') ->
@@ -168,27 +200,27 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
168200 mostUnique (Pi _ _ pk : es) k = mostUnique es (smaller pk k)
169201 mostUnique (_ : es) k = mostUnique es k
170202
171− chk u env (RBind n b sc)
203+ chk u maxu env (RBind n b sc)
172204 = do (b', bt') <- checkBinder b
173− (scv, sct) <- chk (smaller bt' u) ((n, b'):env) sc
205+ (scv, sct) <- chk (smaller bt' u) maxu ((n, b'):env) sc
174206 discharge n b' bt' (pToV n scv) (pToV n sct)
175207 where checkBinder (Lam t)
176− = do (tv, tt) <- chk u env t
208+ = do (tv, tt) <- chk u maxu env t
177209 let tv' = normalise ctxt env tv
178210 let tt' = normalise ctxt env tt
179211 lift $ isType ctxt env tt'
180212 return (Lam tv, tt')
181213 checkBinder (Let t v)
182− = do (tv, tt) <- chk u env t
183− (vv, vt) <- chk u env v
214+ = do (tv, tt) <- chk u maxu env t
215+ (vv, vt) <- chk u maxu env v
184216 let tv' = normalise ctxt env tv
185217 let tt' = normalise ctxt env tt
186218 convertsC ctxt env vt tv
187219 lift $ isType ctxt env tt'
188220 return (Let tv vv, tt')
189221 checkBinder (NLet t v)
190− = do (tv, tt) <- chk u env t
191− (vv, vt) <- chk u env v
222+ = do (tv, tt) <- chk u maxu env t
223+ (vv, vt) <- chk u maxu env v
192224 let tv' = normalise ctxt env tv
193225 let tt' = normalise ctxt env tt
194226 convertsC ctxt env vt tv
@@ -197,13 +229,13 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
197229 checkBinder (Hole t)
198230 | not holes = lift $ tfail (IncompleteTerm undefined)
199231 | otherwise
200− = do (tv, tt) <- chk u env t
232+ = do (tv, tt) <- chk u maxu env t
201233 let tv' = normalise ctxt env tv
202234 let tt' = normalise ctxt env tt
203235 lift $ isType ctxt env tt'
204236 return (Hole tv, tt')
205237 checkBinder (GHole i t)
206− = do (tv, tt) <- chk u env t
238+ = do (tv, tt) <- chk u maxu env t
207239 let tv' = normalise ctxt env tv
208240 let tt' = normalise ctxt env tt
209241 lift $ isType ctxt env tt'
@@ -211,15 +243,15 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
211243 checkBinder (Guess t v)
212244 | not holes = lift $ tfail (IncompleteTerm undefined)
213245 | otherwise
214− = do (tv, tt) <- chk u env t
215− (vv, vt) <- chk u env v
246+ = do (tv, tt) <- chk u maxu env t
247+ (vv, vt) <- chk u maxu env v
216248 let tv' = normalise ctxt env tv
217249 let tt' = normalise ctxt env tt
218250 convertsC ctxt env vt tv
219251 lift $ isType ctxt env tt'
220252 return (Guess tv vv, tt')
221253 checkBinder (PVar t)
222− = do (tv, tt) <- chk u env t
254+ = do (tv, tt) <- chk u maxu env t
223255 let tv' = normalise ctxt env tv
224256 let tt' = normalise ctxt env tt
225257 lift $ isType ctxt env tt'
@@ -227,7 +259,7 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
227259 -- to tell if it's a collapsible variable)
228260 return (PVar tv, tt')
229261 checkBinder (PVTy t)
230− = do (tv, tt) <- chk u env t
262+ = do (tv, tt) <- chk u maxu env t
231263 let tv' = normalise ctxt env tv
232264 let tt' = normalise ctxt env tt
233265 lift $ isType ctxt env tt'