Skip to content

Commit 071530c

Browse files
committed
level limit hack: prevent calling ki in term index
We do not call ki for well-kindedness checking from type inference of t1 in type (T {t1}) where (t0 :: T {t1}). That is, when T {t1} is an inferred type of some t0. We would normally call ki to check well kindedness to for terms at the outermost level (i.e. global definitions). * WARNING: this hack is not proved correct, just having leap of faith for now Now, path.miniax only(?) use 1.x GB memory, which is much better than using up more than 3.5 GB memory and amost freezing my laptop.
1 parent 371fba9 commit 071530c

File tree

3 files changed

+59
-60
lines changed

3 files changed

+59
-60
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
mininax
22
=======
33

4-
* WARNING: type checking test/path.mininax will eat up most of your 4GB memory
5-
64
Mininax is a prototype reference implementation of the Nax programming language,
75
which is described in [my Ph.D. dissertation draft](https://dl.dropboxusercontent.com/u/2589099/thesis/Nax_KiYungAhn_thesis_draft.pdf),
86
but only implements the core part of the language without syntactic sugars

src/Infer.hs

+55-54
Original file line numberDiff line numberDiff line change
@@ -149,46 +149,46 @@ type KI = FreshMT (StateT [(TyName,Ki)]
149149

150150
type KiSch = Bind ([TmName],[TyName],[KiName]) Ki
151151

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)
154154
| head(show x) == '`' = throwError(strMsg $ show x++
155155
" backquoted variable not allowed (ki)")
156-
ki kctx ictx env (Var x) =
156+
ki n kctx ictx env (Var x) =
157157
case lookup x kctx of
158158
Just kisch -> return =<< freshKiInst kisch -- ki vars must be simple though
159159
Nothing -> do
160160
ps <- lift get
161161
case lookup x ps of
162162
Just k -> return k
163163
Nothing -> throwError(strMsg $ "ty var "++show x++" undefined tyvar")
164-
ki kctx ictx env (TCon x) =
164+
ki n kctx ictx env (TCon x) =
165165
case lookup x kctx of
166166
Just kisch -> return =<< freshKiInst kisch
167167
Nothing -> do
168168
ps <- lift get
169169
case lookup x ps of
170170
Just k -> return k
171171
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
175175
lift2 $ unify Star k1
176176
lift2 $ unify Star k2
177177
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
181181
k <- Var <$> fresh "k"
182182
lift2 $ unify (KArr (Right k2) k) k1
183183
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
187187
k <- Var <$> fresh "k"
188188
lift2 $ unify (KArr (Left t2) k) k1
189189
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
192192
k <- Var <$> fresh "k"
193193
lift2 $ unify (KArr (Right k) k) k1
194194
return k
@@ -312,43 +312,43 @@ unfoldTApp ty = [Right ty]
312312
eitherVar = either (Left . Var) (Right . Var)
313313

314314

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)
317317
| head(show x) == '`' = throwError(strMsg $ show x++
318318
" backquoted variable not allowed (ti)")
319-
ti kctx ictx ctx env (Var x) =
319+
ti n kctx ictx ctx env (Var x) =
320320
case lookup x (ctx++ictx) of
321321
Just tysch -> return =<< freshTyInst tysch
322322
Nothing -> do
323323
ps <- lift get
324324
case lookup x ps of
325325
Just t -> return t
326326
Nothing -> throwError(strMsg $ show x++" undefined var")
327-
ti kctx ictx ctx env (Con x) =
327+
ti n kctx ictx ctx env (Con x) =
328328
case lookup x ictx of
329329
Just tysch -> return =<< freshTyInst tysch
330330
Nothing -> do
331331
ps <- lift get
332332
case lookup x ps of
333333
Just t -> return t
334334
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")
337337
| otherwise =
338-
do ty <- ti kctx ictx ctx env t
338+
do ty <- ti n kctx ictx ctx env t
339339
`catchErrorThrowWithMsg`
340340
(++ "\n\t" ++ "when checking type of " ++ show t)
341-
let m = fromInteger n
341+
let m_ = fromInteger m
342342
foldr mplus (throwError(strMsg $ show e ++ " has incorrect number")) $ do
343343
-- 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" ]
346346
return $ do -- fresh monad
347347
is <- sequence mis
348348
ty1 <- Var <$> freshTyName' "t"
349349
lift2 $ unify (foldl TApp ty1 (Right (TFix ty1) : is)) ty
350350
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) ++ " %%%%%%%%%%%%%%%% ") $
352352
do (f, Alt mphi as) <- unbind b
353353
r <- fresh "_r"
354354
t <- freshTyName' "t"
@@ -367,7 +367,7 @@ ti kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% ") $
367367
let ctx' = (f,tyfsch) : ctx
368368
() <- trace ("\tkctx' = "++show kctx') $ return ()
369369
() <- trace ("\tctx' = "++show ctx') $ return ()
370-
tytm' <- tiAlts kctx' ictx ctx' env (Alt mphi' as)
370+
tytm' <- tiAlts n kctx' ictx ctx' env (Alt mphi' as)
371371
lift2 $ unify tytm tytm'
372372
u <- lift getSubst
373373
let ty = uapply u $
@@ -376,7 +376,7 @@ ti kctx ictx ctx env (MIt b) = trace (show (MIt b) ++ " %%%%%%%%%%%%%%%% ") $
376376
"abstract type variable "++show r++" cannot escape in type "++
377377
show ty ++" of "++show(MIt b) )
378378
return ty
379-
ti kctx ictx ctx env (MPr b) =
379+
ti n kctx ictx ctx env (MPr b) =
380380
do ((f,cast), Alt mphi as) <- unbind b
381381
r <- fresh "_r"
382382
t <- freshTyName' "t"
@@ -396,7 +396,7 @@ ti kctx ictx ctx env (MPr b) =
396396
closeTy kctx' ictx tyret
397397
return $ bind (union is vs) tyf
398398
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)
400400
lift2 $ unify tytm tytm'
401401
u <- lift getSubst
402402
let ty = uapply u $
@@ -405,16 +405,16 @@ ti kctx ictx ctx env (MPr b) =
405405
"abstract type variable "++show r++" cannot escape in type "++
406406
show ty ++" of "++show(MPr b) )
407407
return ty
408-
ti kctx ictx ctx env (Lam b) =
408+
ti n kctx ictx ctx env (Lam b) =
409409
do (x, t) <- unbind b
410410
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
412412
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
415415
`catchErrorThrowWithMsg`
416416
(++ "\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
418418
`catchErrorThrowWithMsg`
419419
(++ "\n\t" ++ "when checking type of " ++ show t2
420420
++ "\n" ++ "kctx = " ++ show kctx
@@ -423,32 +423,33 @@ ti kctx ictx ctx env (App t1 t2) =
423423
)
424424
ty <- Var <$> freshTyName "a" Star
425425
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
430431
return ty
431-
ti kctx ictx ctx env (Let b) =
432+
ti n kctx ictx ctx env (Let b) =
432433
do ((x, Embed t1), t2) <- unbind b
433-
ty <- ti kctx ictx ctx env t1
434+
ty <- ti n kctx ictx ctx env t1
434435
`catchErrorThrowWithMsg`
435436
(++ "\n\t" ++ "when checking type of " ++ show t1)
436437
u <- lift getSubst
437438
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) =
442443
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)
444445

445446

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
448449
lift2 $ unifyMany (zip tys (tail tys))
449450
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
452453
u <- lift getSubst
453454
let (Right tcon : args) =
454455
tApp2list $ case (head tys) of TArr t _ -> uapply u t
@@ -489,7 +490,7 @@ app2list (App t1 t2) = app2list t1 ++ [t2]
489490
app2list t = [t]
490491

491492

492-
tiAlt kctx ictx ctx env mphi (x,b) =
493+
tiAlt n kctx ictx ctx env mphi (x,b) =
493494
do xTy <- case lookup x ictx of
494495
Nothing -> throwError . strMsg $ show x ++ " undefined"
495496
Just xt -> freshTyInst xt
@@ -552,11 +553,11 @@ tiAlt kctx ictx ctx env mphi (x,b) =
552553
(ns,t) <- unbind b
553554
let ctx' = trace (show ns ++", "++ show xtyArgs') $ zip ns (map monoTy xtyArgs') ++ ctx
554555
() <- 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))
556557
`catchErrorThrowWithMsg`
557558
(++ "\n\t" ++ "when checking type of "
558559
++ 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
560561
`catchErrorThrowWithMsg`
561562
(++ "\n\t" ++ "when checking type of " ++ show t)
562563
() <- trace ("zzaaa2\t"++show xtyRet++" =?= "++show domty) $ return ()
@@ -631,9 +632,9 @@ runTI = runTIwith nullState []
631632
runTIwith stUS st = runUSwith stUS . runErrorT . flip evalStateT st . runFreshMT
632633

633634

634-
ti' ctx = runTI . ti [] [] [] ctx
635+
ti' ctx = runTI . ti 0 [] [] [] ctx
635636

636-
ty = runTI $ ti [] [] [] [] (lam "x" (Var "x"))
637+
ty = runTI $ ti 0 [] [] [] [] (lam "x" (Var "x"))
637638

638639

639640
unbindSch sch = snd (unsafeUnbind sch)

src/InferDec.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ tiDec (Def (LIdent x) t) (kctx,ictx,env)
7070
" backquoted variable not allowed")
7171
tiDec (Def (LIdent x) t) (kctx,ictx,env) = trace ("\nDef "++ show x++" *****") $
7272
do let tm = term2Tm' env t
73-
ty <- ti kctx ictx [] env tm
73+
ty <- ti 0 kctx ictx [] env tm
7474
`catchErrorThrowWithMsg`
7575
(++ "\n\t" ++ "when checking defintion of " ++ x)
7676
u <- lift getSubst
@@ -159,7 +159,7 @@ tiDec (Gadt (UIdent tc) as k gAlts) (kctx,ictx,env) =
159159

160160
kiDAlt :: KCtx -> Ctx -> Env -> DataAlt -> KI ()
161161
kiDAlt kctx ictx env (DAlt _ ts) =
162-
do ks <- mapM (ki kctx ictx env) (map (type2Ty' env) ts)
162+
do ks <- mapM (ki 0 kctx ictx env) (map (type2Ty' env) ts)
163163
lift2 $ unifyMany (zip (repeat Star) ks)
164164
where
165165

@@ -187,11 +187,11 @@ kiGAlt (tc,kisch) as kctx ictx env (GAlt (UIdent c) t) =
187187
ictx' <- (++ ictx) <$> sequence [(,) x <$> freshTy | x <- fvTm']
188188
() <- trace ("kctx' = "++show kctx') $ return ()
189189
() <- trace ("ictx' = "++show ictx') $ return ()
190-
k <- ki ((tc,kisch):kctx') ictx' env resTy'
190+
k <- ki 0 ((tc,kisch):kctx') ictx' env resTy'
191191
`catchErrorThrowWithMsg`
192192
(++ "\n\t" ++ "when checking kind of resTy' " ++ show resTy')
193193
() <- trace ("wwwwww222") $ return ()
194-
ks <- mapM (ki kctx' ictx' env) ts'
194+
ks <- mapM (ki 0 kctx' ictx' env) ts'
195195
() <- trace ("wwwwww333") $ return ()
196196
lift2 $ unifyMany (zip (repeat Star) (k:ks))
197197
u <- lift getSubst

0 commit comments

Comments
 (0)