@@ -69,7 +69,7 @@ check ctxt env tm
69 = evalStateT (check' True ctxt env tm) (0, []) -- Holes allowed
69 = evalStateT (check' True ctxt env tm) (0, []) -- Holes allowed
71check' :: Bool -> Context -> Env -> Raw -> StateT UCs TC (Term, Type)
71check' :: 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
74 smaller (UType NullType) _ = UType NullType
74 smaller (UType NullType) _ = UType NullType
75 smaller _ (UType NullType) = UType NullType
75 smaller _ (UType NullType) = UType NullType
@@ -78,23 +78,47 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
78 smaller x _ = x
78 smaller x _ = x
80 chk :: Type -> -- uniqueness level
80 chk :: Type -> -- uniqueness level
81+ Maybe UExp -> -- maximum universe level, if known
81 Env -> Raw -> StateT UCs TC (Term, Type)
82 Env -> Raw -> StateT UCs TC (Term, Type)
82− chk u env (Var n)
83+ chk u maxu env (Var n)
83 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
84 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
84 -- If we're elaborating, we don't want the private names; if we're
85 -- If we're elaborating, we don't want the private names; if we're
85 -- checking an already elaborated term, we do
86 -- checking an already elaborated term, we do
86 | (P nt n' ty : _) <- lookupP_all (not holes) n ctxt
87 | (P nt n' ty : _) <- lookupP_all (not holes) n ctxt
87 = return (P nt n' ty, ty)
88 = return (P nt n' ty, ty)
88 | otherwise = do lift $ tfail $ NoSuchVariable n
89 | otherwise = do lift $ tfail $ NoSuchVariable n
89− chk u env ap@(RApp f a)
90+ chk u maxu env ap@(RApp f RType) | not holes
91+ -- special case to reduce constraintss
90− = do (fv, fty) <- chk u env f
92+ = do (fv, fty) <- chk u maxu env f
91− (av, aty) <- chk u env a
92 let fty' = case uniqueBinders (map fst env) (finalise fty) of
93 let fty' = case uniqueBinders (map fst env) (finalise fty) of
93 ty@(Bind x (Pi i s k) t) -> ty
94 ty@(Bind x (Pi i s k) t) -> ty
94 _ -> uniqueBinders (map fst env)
95 _ -> uniqueBinders (map fst env)
95 $ case hnf ctxt env fty of
96 $ case hnf ctxt env fty of
96 ty@(Bind x (Pi i s k) t) -> ty
97 ty@(Bind x (Pi i s k) t) -> ty
97 _ -> normalise ctxt env fty
98 _ -> normalise ctxt env fty
100+ Bind x (Pi i (TType v') k) t ->
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
98 case fty' of
122 case fty' of
99 Bind x (Pi i s k) t ->
123 Bind x (Pi i s k) t ->
100 do convertsC ctxt env aty s
124 do convertsC ctxt env aty s
@@ -102,13 +126,13 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
102 (Bind x (Let aty av) t)
126 (Bind x (Let aty av) t)
103 return (App fv av, apty)
127 return (App fv av, apty)
104 t -> lift $ tfail $ NonFunctionType fv fty
128 t -> lift $ tfail $ NonFunctionType fv fty
105− chk u env RType
129+ chk u maxu env RType
106 | holes = return (TType (UVal 0), TType (UVal 0))
130 | holes = return (TType (UVal 0), TType (UVal 0))
107 | otherwise = do (v, cs) <- get
131 | otherwise = do (v, cs) <- get
108 let c = ULT (UVar v) (UVar (v+1))
132 let c = ULT (UVar v) (UVar (v+1))
109 put (v+2, (c:cs))
133 put (v+2, (c:cs))
110 return (TType (UVar v), TType (UVar (v+1)))
134 return (TType (UVar v), TType (UVar (v+1)))
111− chk u env (RUType un)
135+ chk u maxu env (RUType un)
112 | holes = return (UType un, TType (UVal 0))
136 | holes = return (UType un, TType (UVal 0))
113 | otherwise = do -- TODO! Issue #1715 on the issue tracker.
137 | otherwise = do -- TODO! Issue #1715 on the issue tracker.
114 -- https://github.com/idris-lang/Idris-dev/issues/1715
138 -- 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
117 -- put (v+2, (c:cs))
141 -- put (v+2, (c:cs))
118 -- return (TType (UVar v), TType (UVar (v+1)))
142 -- return (TType (UVar v), TType (UVar (v+1)))
119 return (UType un, TType (UVal 0))
143 return (UType un, TType (UVal 0))
120− chk u env (RConstant Forgot) = return (Erased, Erased)
144+ chk u maxu env (RConstant Forgot) = return (Erased, Erased)
121− chk u env (RConstant c) = return (Constant c, constType c)
145+ chk u maxu env (RConstant c) = return (Constant c, constType c)
122 where constType (I _) = Constant (AType (ATInt ITNative))
146 where constType (I _) = Constant (AType (ATInt ITNative))
123 constType (BI _) = Constant (AType (ATInt ITBig))
147 constType (BI _) = Constant (AType (ATInt ITBig))
124 constType (Fl _) = Constant (AType ATFloat)
148 constType (Fl _) = Constant (AType ATFloat)
@@ -135,17 +159,25 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
135 constType TheWorld = Constant WorldType
159 constType TheWorld = Constant WorldType
136 constType Forgot = Erased
160 constType Forgot = Erased
137 constType _ = TType (UVal 0)
161 constType _ = TType (UVal 0)
138− chk u env (RForce t) = do (_, ty) <- chk u env t
162+ chk u maxu env (RForce t) = do (_, ty) <- chk u maxu env t
139− return (Erased, ty)
163+ return (Erased, ty)
140− chk u env (RBind n (Pi i s k) t)
164+ chk u maxu env (RBind n (Pi i s k) t)
141− = do (sv, st) <- chk u env s
165+ = do (sv, st) <- chk u Nothing env s
142− (kv, kt) <- chk u env k
167+ (kv, kt) <- chk u Nothing env k
143− (tv, tt) <- chk st ((n, Pi i sv kv) : env) t
168+ put vcs -- no need to validate these constraints, they are independent
144 (v, cs) <- get
169 (v, cs) <- get
170+ nmaxu <- case maxu of
171+ Nothing -> do put (v+1, cs)
174+ (tv, tt) <- chk st (Just nmaxu) ((n, Pi i sv kv) : env) t
145 case (normalise ctxt env st, normalise ctxt env tt) of
175 case (normalise ctxt env st, normalise ctxt env tt) of
146 (TType su, TType tu) -> do
176 (TType su, TType tu) -> do
147− when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
177+ when (not holes) $ do (v, cs) <- get
178+ put (v, ULE su nmaxu :
148− let k' = st `smaller` kv `smaller` TType (UVar v) `smaller` u
180+ let k' = TType (UVar v) `smaller` st `smaller` kv `smaller` u
149 return (Bind n (Pi i (uniqueBinders (map fst env) sv) k')
181 return (Bind n (Pi i (uniqueBinders (map fst env) sv) k')
150 (pToV n tv), k')
182 (pToV n tv), k')
151 (un, un') ->
183 (un, un') ->
@@ -168,27 +200,27 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
168 mostUnique (Pi _ _ pk : es) k = mostUnique es (smaller pk k)
200 mostUnique (Pi _ _ pk : es) k = mostUnique es (smaller pk k)
169 mostUnique (_ : es) k = mostUnique es k
201 mostUnique (_ : es) k = mostUnique es k
171− chk u env (RBind n b sc)
203+ chk u maxu env (RBind n b sc)
172 = do (b', bt') <- checkBinder b
204 = 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
174 discharge n b' bt' (pToV n scv) (pToV n sct)
206 discharge n b' bt' (pToV n scv) (pToV n sct)
175 where checkBinder (Lam t)
207 where checkBinder (Lam t)
176− = do (tv, tt) <- chk u env t
208+ = do (tv, tt) <- chk u maxu env t
177 let tv' = normalise ctxt env tv
209 let tv' = normalise ctxt env tv
178 let tt' = normalise ctxt env tt
210 let tt' = normalise ctxt env tt
179 lift $ isType ctxt env tt'
211 lift $ isType ctxt env tt'
180 return (Lam tv, tt')
212 return (Lam tv, tt')
181 checkBinder (Let t v)
213 checkBinder (Let t v)
182− = do (tv, tt) <- chk u env t
214+ = do (tv, tt) <- chk u maxu env t
183− (vv, vt) <- chk u env v
215+ (vv, vt) <- chk u maxu env v
184 let tv' = normalise ctxt env tv
216 let tv' = normalise ctxt env tv
185 let tt' = normalise ctxt env tt
217 let tt' = normalise ctxt env tt
186 convertsC ctxt env vt tv
218 convertsC ctxt env vt tv
187 lift $ isType ctxt env tt'
219 lift $ isType ctxt env tt'
188 return (Let tv vv, tt')
220 return (Let tv vv, tt')
189 checkBinder (NLet t v)
221 checkBinder (NLet t v)
190− = do (tv, tt) <- chk u env t
222+ = do (tv, tt) <- chk u maxu env t
191− (vv, vt) <- chk u env v
223+ (vv, vt) <- chk u maxu env v
192 let tv' = normalise ctxt env tv
224 let tv' = normalise ctxt env tv
193 let tt' = normalise ctxt env tt
225 let tt' = normalise ctxt env tt
194 convertsC ctxt env vt tv
226 convertsC ctxt env vt tv
@@ -197,13 +229,13 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
197 checkBinder (Hole t)
229 checkBinder (Hole t)
198 | not holes = lift $ tfail (IncompleteTerm undefined)
230 | not holes = lift $ tfail (IncompleteTerm undefined)
199 | otherwise
231 | otherwise
200− = do (tv, tt) <- chk u env t
232+ = do (tv, tt) <- chk u maxu env t
201 let tv' = normalise ctxt env tv
233 let tv' = normalise ctxt env tv
202 let tt' = normalise ctxt env tt
234 let tt' = normalise ctxt env tt
203 lift $ isType ctxt env tt'
235 lift $ isType ctxt env tt'
204 return (Hole tv, tt')
236 return (Hole tv, tt')
205 checkBinder (GHole i t)
237 checkBinder (GHole i t)
206− = do (tv, tt) <- chk u env t
238+ = do (tv, tt) <- chk u maxu env t
207 let tv' = normalise ctxt env tv
239 let tv' = normalise ctxt env tv
208 let tt' = normalise ctxt env tt
240 let tt' = normalise ctxt env tt
209 lift $ isType ctxt env tt'
241 lift $ isType ctxt env tt'
@@ -211,15 +243,15 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
211 checkBinder (Guess t v)
243 checkBinder (Guess t v)
212 | not holes = lift $ tfail (IncompleteTerm undefined)
244 | not holes = lift $ tfail (IncompleteTerm undefined)
213 | otherwise
245 | otherwise
214− = do (tv, tt) <- chk u env t
246+ = do (tv, tt) <- chk u maxu env t
215− (vv, vt) <- chk u env v
247+ (vv, vt) <- chk u maxu env v
216 let tv' = normalise ctxt env tv
248 let tv' = normalise ctxt env tv
217 let tt' = normalise ctxt env tt
249 let tt' = normalise ctxt env tt
218 convertsC ctxt env vt tv
250 convertsC ctxt env vt tv
219 lift $ isType ctxt env tt'
251 lift $ isType ctxt env tt'
220 return (Guess tv vv, tt')
252 return (Guess tv vv, tt')
221 checkBinder (PVar t)
253 checkBinder (PVar t)
222− = do (tv, tt) <- chk u env t
254+ = do (tv, tt) <- chk u maxu env t
223 let tv' = normalise ctxt env tv
255 let tv' = normalise ctxt env tv
224 let tt' = normalise ctxt env tt
256 let tt' = normalise ctxt env tt
225 lift $ isType ctxt env tt'
257 lift $ isType ctxt env tt'
@@ -227,7 +259,7 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
227 -- to tell if it's a collapsible variable)
259 -- to tell if it's a collapsible variable)
228 return (PVar tv, tt')
260 return (PVar tv, tt')
229 checkBinder (PVTy t)
261 checkBinder (PVTy t)
230− = do (tv, tt) <- chk u env t
262+ = do (tv, tt) <- chk u maxu env t
231 let tv' = normalise ctxt env tv
263 let tv' = normalise ctxt env tv
232 let tt' = normalise ctxt env tt
264 let tt' = normalise ctxt env tt
233 lift $ isType ctxt env tt'
265 lift $ isType ctxt env tt'