@@ -149,46 +149,46 @@ type KI = FreshMT (StateT [(TyName,Ki)]
149
149
150
150
type KiSch = Bind ([TmName ],[TyName ],[KiName ]) Ki
151
151
152
- ki :: KCtx -> Ctx -> Env -> Ty -> KI Ki
153
- ki kctx ictx env (Var x)
152
+ ki :: Int -> KCtx -> Ctx -> Env -> Ty -> KI Ki
153
+ ki n kctx ictx env (Var x)
154
154
| head (show x) == ' `' = throwError(strMsg $ show x++
155
155
" backquoted variable not allowed (ki)" )
156
- ki kctx ictx env (Var x) =
156
+ ki n kctx ictx env (Var x) =
157
157
case lookup x kctx of
158
158
Just kisch -> return =<< freshKiInst kisch -- ki vars must be simple though
159
159
Nothing -> do
160
160
ps <- lift get
161
161
case lookup x ps of
162
162
Just k -> return k
163
163
Nothing -> throwError(strMsg $ " ty var " ++ show x++ " undefined tyvar" )
164
- ki kctx ictx env (TCon x) =
164
+ ki n kctx ictx env (TCon x) =
165
165
case lookup x kctx of
166
166
Just kisch -> return =<< freshKiInst kisch
167
167
Nothing -> do
168
168
ps <- lift get
169
169
case lookup x ps of
170
170
Just k -> return k
171
171
Nothing -> throwError(strMsg $ " ty con " ++ show x++ " undefined tycon" )
172
- ki kctx ictx env (TArr t1 t2) =
173
- do k1 <- ki kctx ictx env t1
174
- k2 <- ki kctx ictx env t2
172
+ ki n kctx ictx env (TArr t1 t2) =
173
+ do k1 <- ki n kctx ictx env t1
174
+ k2 <- ki n kctx ictx env t2
175
175
lift2 $ unify Star k1
176
176
lift2 $ unify Star k2
177
177
return Star
178
- ki kctx ictx env (TApp t1 (Right t2)) =
179
- do k1 <- ki kctx ictx env t1
180
- k2 <- ki kctx ictx env t2
178
+ ki n kctx ictx env (TApp t1 (Right t2)) =
179
+ do k1 <- ki n kctx ictx env t1
180
+ k2 <- ki n kctx ictx env t2
181
181
k <- Var <$> fresh " k"
182
182
lift2 $ unify (KArr (Right k2) k) k1
183
183
return k
184
- ki kctx ictx env (TApp t1 (Left e2)) =
185
- do k1 <- ki kctx ictx env t1
186
- t2 <- ti kctx ictx [] env e2
184
+ ki n kctx ictx env (TApp t1 (Left e2)) =
185
+ do k1 <- ki n kctx ictx env t1
186
+ t2 <- ti (n + 1 ) kctx ictx [] env e2
187
187
k <- Var <$> fresh " k"
188
188
lift2 $ unify (KArr (Left t2) k) k1
189
189
return k
190
- ki kctx ictx env (TFix t) =
191
- do k1 <- ki kctx ictx env t
190
+ ki n kctx ictx env (TFix t) =
191
+ do k1 <- ki n kctx ictx env t
192
192
k <- Var <$> fresh " k"
193
193
lift2 $ unify (KArr (Right k) k) k1
194
194
return k
@@ -312,43 +312,43 @@ unfoldTApp ty = [Right ty]
312
312
eitherVar = either (Left . Var ) (Right . Var )
313
313
314
314
315
- ti :: KCtx -> Ctx -> Ctx -> Env -> Tm -> TI Ty
316
- ti kctx ictx ctx env (Var x)
315
+ ti :: Int -> KCtx -> Ctx -> Ctx -> Env -> Tm -> TI Ty
316
+ ti n kctx ictx ctx env (Var x)
317
317
| head (show x) == ' `' = throwError(strMsg $ show x++
318
318
" backquoted variable not allowed (ti)" )
319
- ti kctx ictx ctx env (Var x) =
319
+ ti n kctx ictx ctx env (Var x) =
320
320
case lookup x (ctx++ ictx) of
321
321
Just tysch -> return =<< freshTyInst tysch
322
322
Nothing -> do
323
323
ps <- lift get
324
324
case lookup x ps of
325
325
Just t -> return t
326
326
Nothing -> throwError(strMsg $ show x++ " undefined var" )
327
- ti kctx ictx ctx env (Con x) =
327
+ ti n kctx ictx ctx env (Con x) =
328
328
case lookup x ictx of
329
329
Just tysch -> return =<< freshTyInst tysch
330
330
Nothing -> do
331
331
ps <- lift get
332
332
case lookup x ps of
333
333
Just t -> return t
334
334
Nothing -> throwError(strMsg $ show x++ " undefined con" )
335
- ti kctx ictx ctx env e@ (In n t)
336
- | n < 0 = throwError(strMsg $ show e ++ " has negative number" )
335
+ ti n kctx ictx ctx env e@ (In m t)
336
+ | m < 0 = throwError(strMsg $ show e ++ " has negative number" )
337
337
| otherwise =
338
- do ty <- ti kctx ictx ctx env t
338
+ do ty <- ti n kctx ictx ctx env t
339
339
`catchErrorThrowWithMsg`
340
340
(++ " \n\t " ++ " when checking type of " ++ show t)
341
- let m = fromInteger n
341
+ let m_ = fromInteger m
342
342
foldr mplus (throwError(strMsg $ show e ++ " has incorrect number" )) $ do
343
343
-- list monad (trying all combinations of Right and Left)
344
- mis <- sequence $ replicate m [ Right . Var <$> fresh " k"
345
- , Left . Var <$> freshTyName' " i" ]
344
+ mis <- sequence $ replicate m_ [ Right . Var <$> fresh " k"
345
+ , Left . Var <$> freshTyName' " i" ]
346
346
return $ do -- fresh monad
347
347
is <- sequence mis
348
348
ty1 <- Var <$> freshTyName' " t"
349
349
lift2 $ unify (foldl TApp ty1 (Right (TFix ty1) : is)) ty
350
350
return $ foldl TApp (TFix ty1) is
351
- ti kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% " ) $
351
+ ti n kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% " ) $
352
352
do (f, Alt mphi as) <- unbind b
353
353
r <- fresh " _r"
354
354
t <- freshTyName' " t"
@@ -367,7 +367,7 @@ ti kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% ") $
367
367
let ctx' = (f,tyfsch) : ctx
368
368
() <- trace (" \t kctx' = " ++ show kctx') $ return ()
369
369
() <- trace (" \t ctx' = " ++ show ctx') $ return ()
370
- tytm' <- tiAlts kctx' ictx ctx' env (Alt mphi' as)
370
+ tytm' <- tiAlts n kctx' ictx ctx' env (Alt mphi' as)
371
371
lift2 $ unify tytm tytm'
372
372
u <- lift getSubst
373
373
let ty = uapply u $
@@ -376,7 +376,7 @@ ti kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% ") $
376
376
" abstract type variable " ++ show r++ " cannot escape in type " ++
377
377
show ty ++ " of " ++ show (MIt b) )
378
378
return ty
379
- ti kctx ictx ctx env (MPr b) =
379
+ ti n kctx ictx ctx env (MPr b) =
380
380
do ((f,cast), Alt mphi as) <- unbind b
381
381
r <- fresh " _r"
382
382
t <- freshTyName' " t"
@@ -396,7 +396,7 @@ ti kctx ictx ctx env (MPr b) =
396
396
closeTy kctx' ictx tyret
397
397
return $ bind (union is vs) tyf
398
398
let ctx' = (f,tyfsch) : (cast,bind is tycast) : ctx
399
- tytm' <- tiAlts kctx' ictx ctx' env (Alt mphi' as)
399
+ tytm' <- tiAlts n kctx' ictx ctx' env (Alt mphi' as)
400
400
lift2 $ unify tytm tytm'
401
401
u <- lift getSubst
402
402
let ty = uapply u $
@@ -405,16 +405,16 @@ ti kctx ictx ctx env (MPr b) =
405
405
" abstract type variable " ++ show r++ " cannot escape in type " ++
406
406
show ty ++ " of " ++ show (MPr b) )
407
407
return ty
408
- ti kctx ictx ctx env (Lam b) =
408
+ ti n kctx ictx ctx env (Lam b) =
409
409
do (x, t) <- unbind b
410
410
ty1 <- Var <$> freshTyName " _" Star
411
- ty2 <- ti kctx ictx ((x, monoTy ty1) : ctx) env t
411
+ ty2 <- ti n kctx ictx ((x, monoTy ty1) : ctx) env t
412
412
return (TArr ty1 ty2)
413
- ti kctx ictx ctx env (App t1 t2) =
414
- do ty1 <- ti kctx ictx ctx env t1
413
+ ti n kctx ictx ctx env (App t1 t2) =
414
+ do ty1 <- ti n kctx ictx ctx env t1
415
415
`catchErrorThrowWithMsg`
416
416
(++ " \n\t " ++ " when checking type of " ++ show t1)
417
- ty2 <- ti kctx ictx ctx env t2
417
+ ty2 <- ti n kctx ictx ctx env t2
418
418
`catchErrorThrowWithMsg`
419
419
(++ " \n\t " ++ " when checking type of " ++ show t2
420
420
++ " \n " ++ " kctx = " ++ show kctx
@@ -423,32 +423,33 @@ ti kctx ictx ctx env (App t1 t2) =
423
423
)
424
424
ty <- Var <$> freshTyName " a" Star
425
425
lift2 $ unify (TArr ty2 ty) ty1
426
- () <- trace (" KIND THING in " ++ show (App t1 t2)) $ return ()
427
- u <- lift getSubst
428
- k <- ki kctx ictx env (uapply u ty)
429
- lift2 $ unify k Star
426
+ when (n == 0 ) $ do
427
+ () <- trace (" KIND THING in " ++ show (App t1 t2)) $ return ()
428
+ u <- lift getSubst
429
+ k <- ki n kctx ictx env (uapply u ty)
430
+ lift2 $ unify k Star
430
431
return ty
431
- ti kctx ictx ctx env (Let b) =
432
+ ti n kctx ictx ctx env (Let b) =
432
433
do ((x, Embed t1), t2) <- unbind b
433
- ty <- ti kctx ictx ctx env t1
434
+ ty <- ti n kctx ictx ctx env t1
434
435
`catchErrorThrowWithMsg`
435
436
(++ " \n\t " ++ " when checking type of " ++ show t1)
436
437
u <- lift getSubst
437
438
tysch <- closeTy kctx (ictx++ ctx) (uapply u ty)
438
- ti kctx ictx ((x, tysch) : ctx) env t2
439
- ti kctx ictx ctx env (Alt _ [] ) = throwError(strMsg " empty Alts" )
440
- ti kctx ictx ctx env e@ (Alt Nothing as) = tiAlts kctx ictx ctx env e
441
- ti kctx ictx ctx env (Alt (Just phi) as) =
439
+ ti n kctx ictx ((x, tysch) : ctx) env t2
440
+ ti n kctx ictx ctx env (Alt _ [] ) = throwError(strMsg " empty Alts" )
441
+ ti n kctx ictx ctx env e@ (Alt Nothing as) = tiAlts n kctx ictx ctx env e
442
+ ti n kctx ictx ctx env (Alt (Just phi) as) =
442
443
do phi <- freshenPhi kctx ictx phi
443
- tiAlts kctx ictx ctx env (Alt (Just phi) as)
444
+ tiAlts n kctx ictx ctx env (Alt (Just phi) as)
444
445
445
446
446
- tiAlts kctx ictx ctx env (Alt Nothing as) = -- TODO coverage of all ctors
447
- do tys <- mapM (tiAlt kctx ictx ctx env Nothing ) as
447
+ tiAlts n kctx ictx ctx env (Alt Nothing as) = -- TODO coverage of all ctors
448
+ do tys <- mapM (tiAlt n kctx ictx ctx env Nothing ) as
448
449
lift2 $ unifyMany (zip tys (tail tys))
449
450
return (head tys)
450
- tiAlts kctx ictx ctx env (Alt (Just phi) as) = -- TODO coverage of all ctors
451
- do tys <- mapM (tiAlt kctx ictx ctx env (Just phi)) as
451
+ tiAlts n kctx ictx ctx env (Alt (Just phi) as) = -- TODO coverage of all ctors
452
+ do tys <- mapM (tiAlt n kctx ictx ctx env (Just phi)) as
452
453
u <- lift getSubst
453
454
let (Right tcon : args) =
454
455
tApp2list $ case (head tys) of TArr t _ -> uapply u t
@@ -489,7 +490,7 @@ app2list (App t1 t2) = app2list t1 ++ [t2]
489
490
app2list t = [t]
490
491
491
492
492
- tiAlt kctx ictx ctx env mphi (x,b) =
493
+ tiAlt n kctx ictx ctx env mphi (x,b) =
493
494
do xTy <- case lookup x ictx of
494
495
Nothing -> throwError . strMsg $ show x ++ " undefined"
495
496
Just xt -> freshTyInst xt
@@ -552,11 +553,11 @@ tiAlt kctx ictx ctx env mphi (x,b) =
552
553
(ns,t) <- unbind b
553
554
let ctx' = trace (show ns ++ " , " ++ show xtyArgs') $ zip ns (map monoTy xtyArgs') ++ ctx
554
555
() <- trace " zzaaa" $ return ()
555
- domty <- ti kctx' ictx' ctx' env (foldl1 App (Con x : map Var ns))
556
+ domty <- ti n kctx' ictx' ctx' env (foldl1 App (Con x : map Var ns))
556
557
`catchErrorThrowWithMsg`
557
558
(++ " \n\t " ++ " when checking type of "
558
559
++ show (foldl1 App (Con x : map Var ns)))
559
- rngty <- ti kctx' ictx' ctx' env t
560
+ rngty <- ti n kctx' ictx' ctx' env t
560
561
`catchErrorThrowWithMsg`
561
562
(++ " \n\t " ++ " when checking type of " ++ show t)
562
563
() <- trace (" zzaaa2\t " ++ show xtyRet++ " =?= " ++ show domty) $ return ()
@@ -631,9 +632,9 @@ runTI = runTIwith nullState []
631
632
runTIwith stUS st = runUSwith stUS . runErrorT . flip evalStateT st . runFreshMT
632
633
633
634
634
- ti' ctx = runTI . ti [] [] [] ctx
635
+ ti' ctx = runTI . ti 0 [] [] [] ctx
635
636
636
- ty = runTI $ ti [] [] [] [] (lam " x" (Var " x" ))
637
+ ty = runTI $ ti 0 [] [] [] [] (lam " x" (Var " x" ))
637
638
638
639
639
640
unbindSch sch = snd (unsafeUnbind sch)
0 commit comments