@@ -22,13 +22,13 @@ import Data.Foldable (foldrM)
2222import LambdaBuffers.Compiler.KindCheck.Context (Context (Context ), addContext , context , getAllContext )
2323import LambdaBuffers.Compiler.KindCheck.Derivation (Derivation (Abstraction , Application , Axiom , Implication ), dTopKind , dType )
2424import LambdaBuffers.Compiler.KindCheck.Judgement (Judgement (Judgement ))
25- import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KType , KVar , (:->:) ))
25+ import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KType , KVar , (:->:) ), protoKind2Kind )
2626import LambdaBuffers.Compiler.KindCheck.Type (Type (Abs , App , Constructor , Opaque , Product , Sum , Var , VoidT ))
2727import LambdaBuffers.Compiler.KindCheck.Variable (Atom , Variable (ForeignRef , TyVar ))
2828
2929import Control.Monad.Freer (Eff , Member , Members , run )
3030import Control.Monad.Freer.Error (Error , runError , throwError )
31- import Control.Monad.Freer.Reader (Reader , ask , asks , runReader )
31+ import Control.Monad.Freer.Reader (Reader , ask , asks , local , runReader )
3232import Control.Monad.Freer.State (State , evalState , get , put )
3333import Control.Monad.Freer.Writer (Writer , runWriter , tell )
3434
@@ -37,7 +37,7 @@ import LambdaBuffers.Compiler.ProtoCompat qualified as PC
3737import Data.String (fromString )
3838import Data.Text qualified as T
3939
40- import Control.Lens ((&) , (.~) , (^.) )
40+ import Control.Lens ((%~) , ( &) , (.~) , (^.) )
4141import Data.Map qualified as M
4242
4343import LambdaBuffers.Compiler.ProtoCompat (localRef2ForeignRef )
@@ -140,16 +140,23 @@ derive x = deriveTyAbs x
140140 case M. toList (tyabs ^. # tyArgs) of
141141 [] -> deriveTyBody (x ^. # tyBody)
142142 a@ (n, _) : as -> do
143- vK <- getBinding ( TyVar n)
143+ vK <- protoKind2Kind <$> getVarAnnotation tyabs n
144144 freshT <- KVar <$> fresh
145+ ctx <- ask
146+
147+ let newContext = ctx & addContext %~ (<> M. singleton (TyVar n) vK)
145148 let newAbs = tyabs & # tyArgs .~ uncurry M. singleton a
146149 let restAbs = tyabs & # tyArgs .~ M. fromList as
147- restF <- deriveTyAbs restAbs
150+
151+ restF <- local (const newContext) $ deriveTyAbs restAbs
152+
148153 let uK = restF ^. dTopKind
149154 tell [Constraint (freshT, uK)]
150- ctx <- ask
151155 pure $ Abstraction (Judgement (ctx, Abs newAbs, vK :->: freshT)) restF
152156
157+ getVarAnnotation :: PC. TyAbs -> PC. VarName -> Derive PC. Kind
158+ getVarAnnotation tyabs varname = pure $ ((tyabs ^. # tyArgs) M. ! varname) ^. # argKind
159+
153160 deriveTyBody :: PC. TyBody -> Derive Derivation
154161 deriveTyBody = \ case
155162 PC. OpaqueI si -> do
0 commit comments