@@ -69,42 +69,51 @@ 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 env top where
72+check' holes ctxt env top = chk (TType (UVar 0)) env top where
74+ smaller (UType NullType) _ = UType NullType
75+ smaller _ (UType NullType) = UType NullType
76+ smaller (UType u) _ = UType u
77+ smaller _ (UType u) = UType u
80+ chk :: Type -> -- uniqueness level
81+ Env -> Raw -> StateT UCs TC (Term, Type)
73− chk env (Var n)
82+ chk u env (Var n)
74 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
83 | Just (i, ty) <- lookupTyEnv n env = return (P Bound n ty, ty)
75 | (P nt n' ty : _) <- lookupP n ctxt = return (P nt n' ty, ty)
84 | (P nt n' ty : _) <- lookupP n ctxt = return (P nt n' ty, ty)
76 | otherwise = do lift $ tfail $ NoSuchVariable n
85 | otherwise = do lift $ tfail $ NoSuchVariable n
77− chk env (RApp f a)
86+ chk u env ap@(RApp f a)
78− = do (fv, fty) <- chk env f
87+ = do (fv, fty) <- chk u env f
79− (av, aty) <- chk env a
88+ (av, aty) <- chk u env a
80 let fty' = case uniqueBinders (map fst env) (finalise fty) of
89 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
82 _ -> uniqueBinders (map fst env)
91 _ -> uniqueBinders (map fst env)
83 $ case hnf ctxt env fty of
92 $ case hnf ctxt env fty of
84− ty@(Bind x (Pi s) t) -> ty
93+ ty@(Bind x (Pi s k) t) -> ty
85 _ -> normalise ctxt env fty
94 _ -> normalise ctxt env fty
86 case fty' of
95 case fty' of
87− Bind x (Pi s) t ->
96+ Bind x (Pi s k) t ->
88 do convertsC ctxt env aty s
97 do convertsC ctxt env aty s
89 let apty = simplify initContext env
98 let apty = simplify initContext env
90 (Bind x (Let aty av) t)
99 (Bind x (Let aty av) t)
91 return (App fv av, apty)
100 return (App fv av, apty)
92 t -> lift $ tfail $ NonFunctionType fv fty
101 t -> lift $ tfail $ NonFunctionType fv fty
93− chk env RType
102+ chk u env RType
94 | holes = return (TType (UVal 0), TType (UVal 0))
103 | holes = return (TType (UVal 0), TType (UVal 0))
95 | otherwise = do (v, cs) <- get
104 | otherwise = do (v, cs) <- get
96 let c = ULT (UVar v) (UVar (v+1))
105 let c = ULT (UVar v) (UVar (v+1))
97 put (v+2, (c:cs))
106 put (v+2, (c:cs))
98 return (TType (UVar v), TType (UVar (v+1)))
107 return (TType (UVar v), TType (UVar (v+1)))
99− chk env (RUType u)
108+ chk u env (RUType un)
100− | holes = return (UType u, TType (UVal 0))
109+ | holes = return (UType un, TType (UVal 0))
101 | otherwise = do -- TODO! (v, cs) <- get
110 | otherwise = do -- TODO! (v, cs) <- get
102 -- let c = ULT (UVar v) (UVar (v+1))
111 -- let c = ULT (UVar v) (UVar (v+1))
103 -- put (v+2, (c:cs))
112 -- put (v+2, (c:cs))
104 -- return (TType (UVar v), TType (UVar (v+1)))
113 -- return (TType (UVar v), TType (UVar (v+1)))
105− return (UType u, TType (UVal 0))
114+ return (UType un, TType (UVal 0))
106− chk env (RConstant Forgot) = return (Erased, Erased)
115+ chk u env (RConstant Forgot) = return (Erased, Erased)
107− chk env (RConstant c) = return (Constant c, constType c)
116+ chk u env (RConstant c) = return (Constant c, constType c)
108 where constType (I _) = Constant (AType (ATInt ITNative))
117 where constType (I _) = Constant (AType (ATInt ITNative))
109 constType (BI _) = Constant (AType (ATInt ITBig))
118 constType (BI _) = Constant (AType (ATInt ITBig))
110 constType (Fl _) = Constant (AType ATFloat)
119 constType (Fl _) = Constant (AType ATFloat)
@@ -120,107 +129,122 @@ check' holes ctxt env top = chk env top where
120 constType (B64V a) = Constant (AType (ATInt (ITVec IT64 (V.length a))))
129 constType (B64V a) = Constant (AType (ATInt (ITVec IT64 (V.length a))))
121 constType Forgot = Erased
130 constType Forgot = Erased
122 constType _ = TType (UVal 0)
131 constType _ = TType (UVal 0)
123− chk env (RForce t) = do (_, ty) <- chk env t
132+ chk u env (RForce t) = do (_, ty) <- chk u env t
124− return (Erased, ty)
133+ return (Erased, ty)
125− chk env (RBind n (Pi s) t)
134+ chk u env (RBind n (Pi s k) t)
126− = do (sv, st) <- chk env s
135+ = do (sv, st) <- chk u env s
136+ (kv, kt) <- chk u env k
127− (tv, tt) <- chk ((n, Pi sv) : env) t
137+ (tv, tt) <- chk st ((n, Pi sv kv) : env) t
138+-- let tv = mkUniquePi kv tv_in
128 (v, cs) <- get
139 (v, cs) <- get
129 case (normalise ctxt env st, normalise ctxt env tt) of
140 case (normalise ctxt env st, normalise ctxt env tt) of
130 (TType su, TType tu) -> do
141 (TType su, TType tu) -> do
131 when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
142 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))
143+ let k' = u `smaller` st `smaller` kv `smaller` TType (UVar v)
133− (pToV n tv), TType (UVar v))
144+ return (Bind n (Pi (uniqueBinders (map fst env) sv) k')
134− (u, u') ->
145+ (pToV n tv), k')
147+ let k' = u `smaller` st `smaller` kv `smaller` un `smaller` un' in
135− return (Bind n (Pi (uniqueBinders (map fst env) sv))
148+ return (Bind n (Pi (uniqueBinders (map fst env) sv) k')
136− (pToV n tv), smaller u u')
149+ (pToV n tv), k')
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)
160+ -- Kind of the whole thing is the kind of the most unique thing
161+ -- in the environment (because uniqueness taints everything...)
163+ mostUnique (Pi _ pk : es) k = mostUnique es (smaller pk k)
164+ mostUnique (_ : es) k = mostUnique es k
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)
166+ chk u env (RBind n b sc)
143− = do b' <- checkBinder b
167+ = do (b', bt') <- checkBinder b
144− (scv, sct) <- chk ((n, b'):env) sc
168+ (scv, sct) <- chk (smaller bt' u) ((n, b'):env) sc
145− discharge n b' (pToV n scv) (pToV n sct)
169+ discharge n b' bt' (pToV n scv) (pToV n sct)
146 where checkBinder (Lam t)
170 where checkBinder (Lam t)
147− = do (tv, tt) <- chk env t
171+ = do (tv, tt) <- chk u env t
148 let tv' = normalise ctxt env tv
172 let tv' = normalise ctxt env tv
149 let tt' = normalise ctxt env tt
173 let tt' = normalise ctxt env tt
150 lift $ isType ctxt env tt'
174 lift $ isType ctxt env tt'
151− return (Lam tv)
175+ return (Lam tv, tt')
152 checkBinder (Let t v)
176 checkBinder (Let t v)
153− = do (tv, tt) <- chk env t
177+ = do (tv, tt) <- chk u env t
154− (vv, vt) <- chk env v
178+ (vv, vt) <- chk u env v
155 let tv' = normalise ctxt env tv
179 let tv' = normalise ctxt env tv
156 let tt' = normalise ctxt env tt
180 let tt' = normalise ctxt env tt
157 convertsC ctxt env vt tv
181 convertsC ctxt env vt tv
158 lift $ isType ctxt env tt'
182 lift $ isType ctxt env tt'
159− return (Let tv vv)
183+ return (Let tv vv, tt')
160 checkBinder (NLet t v)
184 checkBinder (NLet t v)
161− = do (tv, tt) <- chk env t
185+ = do (tv, tt) <- chk u env t
162− (vv, vt) <- chk env v
186+ (vv, vt) <- chk u env v
163 let tv' = normalise ctxt env tv
187 let tv' = normalise ctxt env tv
164 let tt' = normalise ctxt env tt
188 let tt' = normalise ctxt env tt
165 convertsC ctxt env vt tv
189 convertsC ctxt env vt tv
166 lift $ isType ctxt env tt'
190 lift $ isType ctxt env tt'
167− return (NLet tv vv)
191+ return (NLet tv vv, tt')
168 checkBinder (Hole t)
192 checkBinder (Hole t)
169 | not holes = lift $ tfail (IncompleteTerm undefined)
193 | not holes = lift $ tfail (IncompleteTerm undefined)
170 | otherwise
194 | otherwise
171− = do (tv, tt) <- chk env t
195+ = do (tv, tt) <- chk u env t
172 let tv' = normalise ctxt env tv
196 let tv' = normalise ctxt env tv
173 let tt' = normalise ctxt env tt
197 let tt' = normalise ctxt env tt
174 lift $ isType ctxt env tt'
198 lift $ isType ctxt env tt'
175− return (Hole tv)
199+ return (Hole tv, tt')
176 checkBinder (GHole i t)
200 checkBinder (GHole i t)
177− = do (tv, tt) <- chk env t
201+ = do (tv, tt) <- chk u env t
178 let tv' = normalise ctxt env tv
202 let tv' = normalise ctxt env tv
179 let tt' = normalise ctxt env tt
203 let tt' = normalise ctxt env tt
180 lift $ isType ctxt env tt'
204 lift $ isType ctxt env tt'
181− return (GHole i tv)
205+ return (GHole i tv, tt')
182 checkBinder (Guess t v)
206 checkBinder (Guess t v)
183 | not holes = lift $ tfail (IncompleteTerm undefined)
207 | not holes = lift $ tfail (IncompleteTerm undefined)
184 | otherwise
208 | otherwise
185− = do (tv, tt) <- chk env t
209+ = do (tv, tt) <- chk u env t
186− (vv, vt) <- chk env v
210+ (vv, vt) <- chk u env v
187 let tv' = normalise ctxt env tv
211 let tv' = normalise ctxt env tv
188 let tt' = normalise ctxt env tt
212 let tt' = normalise ctxt env tt
189 convertsC ctxt env vt tv
213 convertsC ctxt env vt tv
190 lift $ isType ctxt env tt'
214 lift $ isType ctxt env tt'
191− return (Guess tv vv)
215+ return (Guess tv vv, tt')
192 checkBinder (PVar t)
216 checkBinder (PVar t)
193− = do (tv, tt) <- chk env t
217+ = do (tv, tt) <- chk u env t
194 let tv' = normalise ctxt env tv
218 let tv' = normalise ctxt env tv
195 let tt' = normalise ctxt env tt
219 let tt' = normalise ctxt env tt
196 lift $ isType ctxt env tt'
220 lift $ isType ctxt env tt'
197 -- Normalised version, for erasure purposes (it's easier
221 -- Normalised version, for erasure purposes (it's easier
198 -- to tell if it's a collapsible variable)
222 -- to tell if it's a collapsible variable)
199− return (PVar tv)
223+ return (PVar tv, tt')
200 checkBinder (PVTy t)
224 checkBinder (PVTy t)
201− = do (tv, tt) <- chk env t
225+ = do (tv, tt) <- chk u env t
202 let tv' = normalise ctxt env tv
226 let tv' = normalise ctxt env tv
203 let tt' = normalise ctxt env tt
227 let tt' = normalise ctxt env tt
204 lift $ isType ctxt env tt'
228 lift $ isType ctxt env tt'
205− return (PVTy tv)
229+ return (PVTy tv, tt')
207− discharge n (Lam t) scv sct
231+ discharge n (Lam t) bt scv sct
208− = return (Bind n (Lam t) scv, Bind n (Pi t) sct)
232+ = return (Bind n (Lam t) scv, Bind n (Pi t bt) sct)
209− discharge n (Pi t) scv sct
233+ discharge n (Pi t k) bt scv sct
210− = return (Bind n (Pi t) scv, sct)
234+ = return (Bind n (Pi t k) scv, sct)
211− discharge n (Let t v) scv sct
235+ discharge n (Let t v) bt scv sct
212 = return (Bind n (Let t v) scv, Bind n (Let t v) sct)
236 = 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
214 = return (Bind n (NLet t v) scv, Bind n (Let t v) sct)
238 = 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
216 = return (Bind n (Hole t) scv, sct)
240 = return (Bind n (Hole t) scv, sct)
217− discharge n (GHole i t) scv sct
241+ discharge n (GHole i t) bt scv sct
218 = return (Bind n (GHole i t) scv, sct)
242 = 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
220 = return (Bind n (Guess t v) scv, sct)
244 = return (Bind n (Guess t v) scv, sct)
221− discharge n (PVar t) scv sct
245+ discharge n (PVar t) bt scv sct
222 = return (Bind n (PVar t) scv, Bind n (PVTy t) sct)
246 = 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
224 = return (Bind n (PVTy t) scv, sct)
248 = return (Bind n (PVTy t) scv, sct)
226-- Number of times a name can be used
250-- Number of times a name can be used
@@ -257,11 +281,20 @@ checkUnique borrowed ctxt env tm
257 chkBinders env (App f a) = do chkBinders env f; chkBinders env a
281 chkBinders env (App f a) = do chkBinders env f; chkBinders env a
258 chkBinders env (Bind n b t)
282 chkBinders env (Bind n b t)
259 = do chkBinderName env n b
283 = 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...?)
260 chkBinders ((n, b) : env) t
287 chkBinders ((n, b) : env) t
261 chkBinders env (binderTy b)
288 chkBinders env (binderTy b)
262 case b of
289 case b of
263 Let t v -> chkBinders env v
290 Let t v -> chkBinders env v
264 _ -> return ()
291 _ -> return ()
293+-- chkBinders env (binderTy b)
295+-- Let t v -> chkBinders env v
297+-- chkBinders ((n, b) : env) t
265 chkBinders env t = return ()
298 chkBinders env t = return ()
267 chkBinderName :: Env -> Name -> Binder Term ->
300 chkBinderName :: Env -> Name -> Binder Term ->