@@ -69,42 +69,51 @@ 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 env top where
73− chk env (Var n)
72+check' holes ctxt env top = chk (TType (UVar (-5))) env top where
73+
74+ smaller (UType NullType) _ = UType NullType
75+ smaller _ (UType NullType) = UType NullType
76+ smaller (UType u) _ = UType u
77+ smaller _ (UType u) = UType u
78+ smaller x _ = x
79+
80+ chk :: Type -> -- uniqueness level
81+ Env -> Raw -> StateT UCs TC (Term, Type)
82+ chk u env (Var n)
7483 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
7584 | (P nt n' ty : _) <- lookupP n ctxt = return (P nt n' ty, ty)
7685 | otherwise = do lift $ tfail $ NoSuchVariable n
77− chk env (RApp f a)
78− = do (fv, fty) <- chk env f
79− (av, aty) <- chk env a
86+ chk u env ap@(RApp f a)
87+ = do (fv, fty) <- chk u env f
88+ (av, aty) <- chk u env a
8089 let fty' = case uniqueBinders (map fst env) (finalise fty) of
81− ty@(Bind x (Pi s) t) -> ty
90+ ty@(Bind x (Pi s k) t) -> ty
8291 _ -> uniqueBinders (map fst env)
8392 $ case hnf ctxt env fty of
84− ty@(Bind x (Pi s) t) -> ty
93+ ty@(Bind x (Pi s k) t) -> ty
8594 _ -> normalise ctxt env fty
8695 case fty' of
87− Bind x (Pi s) t ->
96+ Bind x (Pi s k) t ->
8897 do convertsC ctxt env aty s
8998 let apty = simplify initContext env
9099 (Bind x (Let aty av) t)
91100 return (App fv av, apty)
92101 t -> lift $ tfail $ NonFunctionType fv fty
93− chk env RType
102+ chk u env RType
94103 | holes = return (TType (UVal 0), TType (UVal 0))
95104 | otherwise = do (v, cs) <- get
96105 let c = ULT (UVar v) (UVar (v+1))
97106 put (v+2, (c:cs))
98107 return (TType (UVar v), TType (UVar (v+1)))
99− chk env (RUType u)
100− | holes = return (UType u, TType (UVal 0))
108+ chk u env (RUType un)
109+ | holes = return (UType un, TType (UVal 0))
101110 | otherwise = do -- TODO! (v, cs) <- get
102111 -- let c = ULT (UVar v) (UVar (v+1))
103112 -- put (v+2, (c:cs))
104113 -- return (TType (UVar v), TType (UVar (v+1)))
105− return (UType u, TType (UVal 0))
106− chk env (RConstant Forgot) = return (Erased, Erased)
107− chk env (RConstant c) = return (Constant c, constType c)
114+ return (UType un, TType (UVal 0))
115+ chk u env (RConstant Forgot) = return (Erased, Erased)
116+ chk u env (RConstant c) = return (Constant c, constType c)
108117 where constType (I _) = Constant (AType (ATInt ITNative))
109118 constType (BI _) = Constant (AType (ATInt ITBig))
110119 constType (Fl _) = Constant (AType ATFloat)
@@ -120,107 +129,122 @@ check' holes ctxt env top = chk env top where
120129 constType (B64V a) = Constant (AType (ATInt (ITVec IT64 (V.length a))))
121130 constType Forgot = Erased
122131 constType _ = TType (UVal 0)
123− chk env (RForce t) = do (_, ty) <- chk env t
124− return (Erased, ty)
125− chk env (RBind n (Pi s) t)
126− = do (sv, st) <- chk env s
127− (tv, tt) <- chk ((n, Pi sv) : env) t
132+ chk u env (RForce t) = do (_, ty) <- chk u env t
133+ return (Erased, ty)
134+ chk u env (RBind n (Pi s k) t)
135+ = do (sv, st) <- chk u env s
136+ (kv, kt) <- chk u env k
137+ (tv, tt) <- chk st ((n, Pi sv kv) : env) t
138+-- let tv = mkUniquePi kv tv_in
128139 (v, cs) <- get
129140 case (normalise ctxt env st, normalise ctxt env tt) of
130141 (TType su, TType tu) -> do
131142 when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
132− return (Bind n (Pi (uniqueBinders (map fst env) sv))
133− (pToV n tv), TType (UVar v))
134− (u, u') ->
135− return (Bind n (Pi (uniqueBinders (map fst env) sv))
136− (pToV n tv), smaller u u')
143+ let k' = st `smaller` kv `smaller` TType (UVar v) `smaller` u
144+ return (Bind n (Pi (uniqueBinders (map fst env) sv) k')
145+ (pToV n tv), k')
146+ (un, un') ->
147+ let k' = st `smaller` kv `smaller` un `smaller` un' `smaller` u in
148+ return (Bind n (Pi (uniqueBinders (map fst env) sv) k')
149+ (pToV n tv), k')
150+
151+ where mkUniquePi kv (Bind n (Pi s k) sc)
152+ = let k' = smaller kv k in
153+ Bind n (Pi s k') (mkUniquePi k' sc)
154+ mkUniquePi kv (Bind n (Lam t) sc)
155+ = Bind n (Lam (mkUniquePi kv t)) (mkUniquePi kv sc)
156+ mkUniquePi kv (Bind n (Let t v) sc)
157+ = Bind n (Let (mkUniquePi kv t) v) (mkUniquePi kv sc)
158+ mkUniquePi kv t = t
137159
138− where smaller (UType NullType) _ = UType NullType
139− smaller _ (UType NullType) = UType NullType
140− smaller (UType u) _ = UType u
141− smaller _ (UType u) = UType u
142− chk env (RBind n b sc)
143− = do b' <- checkBinder b
144− (scv, sct) <- chk ((n, b'):env) sc
145− discharge n b' (pToV n scv) (pToV n sct)
160+ -- Kind of the whole thing is the kind of the most unique thing
161+ -- in the environment (because uniqueness taints everything...)
162+ mostUnique [] k = k
163+ mostUnique (Pi _ pk : es) k = mostUnique es (smaller pk k)
164+ mostUnique (_ : es) k = mostUnique es k
165+
166+ chk u env (RBind n b sc)
167+ = do (b', bt') <- checkBinder b
168+ (scv, sct) <- chk (smaller bt' u) ((n, b'):env) sc
169+ discharge n b' bt' (pToV n scv) (pToV n sct)
146170 where checkBinder (Lam t)
147− = do (tv, tt) <- chk env t
171+ = do (tv, tt) <- chk u env t
148172 let tv' = normalise ctxt env tv
149173 let tt' = normalise ctxt env tt
150174 lift $ isType ctxt env tt'
151− return (Lam tv)
175+ return (Lam tv, tt')
152176 checkBinder (Let t v)
153− = do (tv, tt) <- chk env t
154− (vv, vt) <- chk env v
177+ = do (tv, tt) <- chk u env t
178+ (vv, vt) <- chk u env v
155179 let tv' = normalise ctxt env tv
156180 let tt' = normalise ctxt env tt
157181 convertsC ctxt env vt tv
158182 lift $ isType ctxt env tt'
159− return (Let tv vv)
183+ return (Let tv vv, tt')
160184 checkBinder (NLet t v)
161− = do (tv, tt) <- chk env t
162− (vv, vt) <- chk env v
185+ = do (tv, tt) <- chk u env t
186+ (vv, vt) <- chk u env v
163187 let tv' = normalise ctxt env tv
164188 let tt' = normalise ctxt env tt
165189 convertsC ctxt env vt tv
166190 lift $ isType ctxt env tt'
167− return (NLet tv vv)
191+ return (NLet tv vv, tt')
168192 checkBinder (Hole t)
169193 | not holes = lift $ tfail (IncompleteTerm undefined)
170194 | otherwise
171− = do (tv, tt) <- chk env t
195+ = do (tv, tt) <- chk u env t
172196 let tv' = normalise ctxt env tv
173197 let tt' = normalise ctxt env tt
174198 lift $ isType ctxt env tt'
175− return (Hole tv)
199+ return (Hole tv, tt')
176200 checkBinder (GHole i t)
177− = do (tv, tt) <- chk env t
201+ = do (tv, tt) <- chk u env t
178202 let tv' = normalise ctxt env tv
179203 let tt' = normalise ctxt env tt
180204 lift $ isType ctxt env tt'
181− return (GHole i tv)
205+ return (GHole i tv, tt')
182206 checkBinder (Guess t v)
183207 | not holes = lift $ tfail (IncompleteTerm undefined)
184208 | otherwise
185− = do (tv, tt) <- chk env t
186− (vv, vt) <- chk env v
209+ = do (tv, tt) <- chk u env t
210+ (vv, vt) <- chk u env v
187211 let tv' = normalise ctxt env tv
188212 let tt' = normalise ctxt env tt
189213 convertsC ctxt env vt tv
190214 lift $ isType ctxt env tt'
191− return (Guess tv vv)
215+ return (Guess tv vv, tt')
192216 checkBinder (PVar t)
193− = do (tv, tt) <- chk env t
217+ = do (tv, tt) <- chk u env t
194218 let tv' = normalise ctxt env tv
195219 let tt' = normalise ctxt env tt
196220 lift $ isType ctxt env tt'
197221 -- Normalised version, for erasure purposes (it's easier
198222 -- to tell if it's a collapsible variable)
199− return (PVar tv)
223+ return (PVar tv, tt')
200224 checkBinder (PVTy t)
201− = do (tv, tt) <- chk env t
225+ = do (tv, tt) <- chk u env t
202226 let tv' = normalise ctxt env tv
203227 let tt' = normalise ctxt env tt
204228 lift $ isType ctxt env tt'
205− return (PVTy tv)
229+ return (PVTy tv, tt')
206230
207− discharge n (Lam t) scv sct
208− = return (Bind n (Lam t) scv, Bind n (Pi t) sct)
209− discharge n (Pi t) scv sct
210− = return (Bind n (Pi t) scv, sct)
211− discharge n (Let t v) scv sct
231+ discharge n (Lam t) bt scv sct
232+ = return (Bind n (Lam t) scv, Bind n (Pi t bt) sct)
233+ discharge n (Pi t k) bt scv sct
234+ = return (Bind n (Pi t k) scv, sct)
235+ discharge n (Let t v) bt scv sct
212236 = return (Bind n (Let t v) scv, Bind n (Let t v) sct)
213− discharge n (NLet t v) scv sct
237+ discharge n (NLet t v) bt scv sct
214238 = return (Bind n (NLet t v) scv, Bind n (Let t v) sct)
215− discharge n (Hole t) scv sct
239+ discharge n (Hole t) bt scv sct
216240 = return (Bind n (Hole t) scv, sct)
217− discharge n (GHole i t) scv sct
241+ discharge n (GHole i t) bt scv sct
218242 = return (Bind n (GHole i t) scv, sct)
219− discharge n (Guess t v) scv sct
243+ discharge n (Guess t v) bt scv sct
220244 = return (Bind n (Guess t v) scv, sct)
221− discharge n (PVar t) scv sct
245+ discharge n (PVar t) bt scv sct
222246 = return (Bind n (PVar t) scv, Bind n (PVTy t) sct)
223− discharge n (PVTy t) scv sct
247+ discharge n (PVTy t) bt scv sct
224248 = return (Bind n (PVTy t) scv, sct)
225249
226250-- Number of times a name can be used
@@ -257,7 +281,20 @@ checkUnique borrowed ctxt env tm
257281 chkBinders env (App f a) = do chkBinders env f; chkBinders env a
258282 chkBinders env (Bind n b t)
259283 = do chkBinderName env n b
284+ -- Must be safe whether we evaluate the scope or binder first
285+ -- (Surely there is a tidier rule than this...?)
286+ st <- get
287+ chkBinders env (binderTy b)
288+ case b of
289+ Let t v -> chkBinders env v
290+ _ -> return ()
260291 chkBinders ((n, b) : env) t
292+-- put st
293+-- chkBinders env (binderTy b)
294+-- case b of
295+-- Let t v -> chkBinders env v
296+-- _ -> return ()
297+-- chkBinders ((n, b) : env) t
261298 chkBinders env t = return ()
262299
263300 chkBinderName :: Env -> Name -> Binder Term ->
@@ -275,10 +312,6 @@ checkUnique borrowed ctxt env tm
275312 UType AllTypes -> do ns <- get
276313 put ((n, (Once, AllTypes)) : ns)
277314 _ -> return ()
278− chkBinders env (binderTy b)
279− case b of
280− Let t v -> chkBinders env v
281− _ -> return ()
282315
283316 chkName n
284317 = do ns <- get