@@ -47,8 +47,8 @@ errEnv = map (\(x, b) -> (x, binderTy b))
4747
4848isType :: Context -> Env -> Term -> TC ()
4949isType ctxt env tm = isType' (normalise ctxt env tm)
50− where isType' (TType _) = return ()
51− isType' tm = fail (showEnv env tm ++ " is not a TType")
50+ where isType' tm | isUniverse tm = return ()
51+ | otherwise = fail (showEnv env tm ++ " is not a Type")
5252
5353recheck :: Context -> Env -> Raw -> Term -> TC (Term, Type, UCs)
5454recheck ctxt env tm orig
@@ -57,10 +57,12 @@ recheck ctxt env tm orig
5757 Error (IncompleteTerm _) -> Error $ IncompleteTerm orig
5858 Error e -> Error e
5959 OK ((tm, ty), constraints) ->
60− return (tm, ty, constraints)
60+ do checkUnique ctxt env tm
61+ return (tm, ty, constraints)
6162
6263check :: Context -> Env -> Raw -> TC (Term, Type)
63−check ctxt env tm = evalStateT (check' True ctxt env tm) (0, []) -- Holes allowed
64+check ctxt env tm
65+ = evalStateT (check' True ctxt env tm) (0, []) -- Holes allowed
6466
6567check' :: Bool -> Context -> Env -> Raw -> StateT UCs TC (Term, Type)
6668check' holes ctxt env top = chk env top where
@@ -102,6 +104,13 @@ check' holes ctxt env top = chk env top where
102104 let c = ULT (UVar v) (UVar (v+1))
103105 put (v+2, (c:cs))
104106 return (TType (UVar v), TType (UVar (v+1)))
107+ chk env (RUType u)
108+ | holes = return (UType u, TType (UVal 0))
109+ | otherwise = do -- TODO! (v, cs) <- get
110+ -- let c = ULT (UVar v) (UVar (v+1))
111+ -- put (v+2, (c:cs))
112+ -- return (TType (UVar v), TType (UVar (v+1)))
113+ return (UType u, TType (UVal 0))
105114 chk env (RConstant Forgot) = return (Erased, Erased)
106115 chk env (RConstant c) = return (Constant c, constType c)
107116 where constType (I _) = Constant (AType (ATInt ITNative))
@@ -125,11 +134,17 @@ check' holes ctxt env top = chk env top where
125134 = do (sv, st) <- chk env s
126135 (tv, tt) <- chk ((n, Pi sv) : env) t
127136 (v, cs) <- get
128− let TType su = normalise ctxt env st
129− let TType tu = normalise ctxt env tt
130− when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
131− return (Bind n (Pi (uniqueBinders (map fst env) sv))
137+ case (normalise ctxt env st, normalise ctxt env tt) of
138+ (TType su, TType tu) -> do
139+ when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
140+ return (Bind n (Pi (uniqueBinders (map fst env) sv))
132141 (pToV n tv), TType (UVar v))
142+ (UType u, _) ->
143+ return (Bind n (Pi (uniqueBinders (map fst env) sv))
144+ (pToV n tv), UType u)
145+ (_, UType u) ->
146+ return (Bind n (Pi (uniqueBinders (map fst env) sv))
147+ (pToV n tv), UType u)
133148 chk env (RBind n b sc)
134149 = do b' <- checkBinder b
135150 (scv, sct) <- chk ((n, b'):env) sc
@@ -140,12 +155,6 @@ check' holes ctxt env top = chk env top where
140155 let tt' = normalise ctxt env tt
141156 lift $ isType ctxt env tt'
142157 return (Lam tv)
143− checkBinder (Pi t)
144− = do (tv, tt) <- chk env t
145− let tv' = normalise ctxt env tv
146− let tt' = normalise ctxt env tt
147− lift $ isType ctxt env tt'
148− return (Pi tv)
149158 checkBinder (Let t v)
150159 = do (tv, tt) <- chk env t
151160 (vv, vt) <- chk env v
@@ -219,3 +228,40 @@ check' holes ctxt env top = chk env top where
219228 = return (Bind n (PVar t) scv, Bind n (PVTy t) sct)
220229 discharge n (PVTy t) scv sct
221230 = return (Bind n (PVTy t) scv, sct)
231+
232+-- If any binders are of kind 'UniqueType' or 'AllTypes' and the name appears
233+-- in the scope more than once, this is an error.
234+checkUnique :: Context -> Env -> Term -> TC ()
235+checkUnique ctxt env tm = evalStateT (chkBinders env (explicitNames tm)) []
236+ where
237+ chkBinders :: Env -> Term -> StateT [(Name, Bool)] TC ()
238+ chkBinders env (V i) | length env > i = chkName (fst (env!!i))
239+ chkBinders env (P _ n _) = chkName n
240+ chkBinders env (App f a) = do chkBinders env f; chkBinders env a
241+ chkBinders env (Bind n b t)
242+ = do chkBinderName env n b
243+ chkBinders ((n, b) : env) t
244+ chkBinders env t = return ()
245+
246+ chkBinderName :: Env -> Name -> Binder Term -> StateT [(Name, Bool)] TC ()
247+ chkBinderName env n b
248+ = do let rawty = forgetEnv (map fst env) (binderTy b)
249+ (_, kind) <- lift $ check ctxt env rawty -- FIXME: Cache in binder?
250+ case kind of
251+ UType UniqueType -> do ns <- get
252+ put ((n, False) : ns)
253+ UType AllTypes -> do ns <- get
254+ put ((n, False) : ns)
255+ _ -> return ()
256+ chkBinders env (binderTy b)
257+ case b of
258+ Let t v -> chkBinders env v
259+ _ -> return ()
260+
261+ chkName n
262+ = do ns <- get
263+ case lookup n ns of
264+ Nothing -> return ()
265+ Just True -> lift $ tfail (UniqueError n)
266+ Just False -> put ((n, True) : filter (\x -> fst x /= n) ns)
267+