@@ -9,7 +9,7 @@ module LambdaBuffers.Compiler.KindCheck (
99 foldWithArrowToType ,
1010) where
1111
12- import Control.Lens ((&) , (.~) , (^.) )
12+ import Control.Lens (view , (&) , (.~) , (^.) )
1313import Control.Monad (void )
1414import Control.Monad.Freer (Eff , Member , Members , interpret , reinterpret , run )
1515import Control.Monad.Freer.Error (Error , runError , throwError )
@@ -21,7 +21,7 @@ import Data.Map qualified as M
2121import LambdaBuffers.Compiler.KindCheck.Context (Context , context )
2222import LambdaBuffers.Compiler.KindCheck.Inference qualified as I
2323import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KType , (:->:) ), kind2ProtoKind )
24- import LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef , LocalRef , TyVar ))
24+ import LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef , TyVar ))
2525import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
2626
2727--------------------------------------------------------------------------------
@@ -110,7 +110,7 @@ localStrategy = reinterpret $ \case
110110runKindCheck :: forall effs {a }. Member Err effs => Eff (KindCheck ': effs ) a -> Eff effs a
111111runKindCheck = interpret $ \ case
112112 -- TypesFromTyDef modName tydef -> runReader modName (tyDef2Types tydef)
113- InferTypeKind modName tyDef ctx k -> either (handleErr modName tyDef) pure $ I. infer ctx tyDef k
113+ InferTypeKind modName tyDef ctx k -> either (handleErr modName tyDef) pure $ I. infer ctx tyDef k modName
114114 CheckKindConsistency modName def ctx k -> runReader modName $ resolveKindConsistency def ctx k
115115 GetSpecifiedKind modName tyDef -> do
116116 (_, k) <- tyDef2NameAndKind modName tyDef
@@ -120,11 +120,15 @@ runKindCheck = interpret $ \case
120120 handleErr modName td = \ case
121121 I. InferUnboundTermErr uA -> do
122122 case uA of
123- LocalRef lr -> throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. LocalI lr) modName
124- ForeignRef fr -> throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. ForeignI fr) modName
123+ ForeignRef fr ->
124+ if (fr ^. # moduleName) == modName
125+ then -- We're looking at the local module.
126+ throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. LocalI $ fr ^. PC. foreignRef2LocalRef) modName
127+ else -- We're looking at a foreign module.
128+ throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. ForeignI fr) modName
125129 TyVar tv -> throwError . PC. CompKindCheckError $ PC. UnboundTyVarError td (PC. TyVar tv) modName
126130 I. InferUnifyTermErr (I. Constraint (k1, k2)) ->
127- throwError . PC. CompKindCheckError $ {- - error $ show k1 <>" <-> " <> show k2 - -} PC. IncorrectApplicationError td (kind2ProtoKind k1) (kind2ProtoKind k2) modName
131+ throwError . PC. CompKindCheckError $ PC. IncorrectApplicationError td (kind2ProtoKind k1) (kind2ProtoKind k2) modName
128132 I. InferRecursiveSubstitutionErr _ ->
129133 throwError . PC. CompKindCheckError $ PC. RecursiveKindError td modName
130134 I. InferImpossibleErr t ->
@@ -207,9 +211,8 @@ tyDefArgs2Context tydef = do
207211 k = pKind2Kind (tyarg ^. # argKind)
208212
209213tyDef2NameAndKind :: forall effs . PC. ModuleName -> PC. TyDef -> Eff effs (Variable , Kind )
210- tyDef2NameAndKind _curModName tyDef = do
211- -- Names are local.
212- let name = LocalRef $ PC. LocalRef (tyDef ^. # tyName) (tyDef ^. # sourceInfo)
214+ tyDef2NameAndKind curModName tyDef = do
215+ let name = ForeignRef $ view (PC. localRef2ForeignRef curModName) $ PC. LocalRef (tyDef ^. # tyName) (tyDef ^. # sourceInfo)
213216 let k = tyAbsLHS2Kind (tyDef ^. # tyAbs)
214217 pure (name, k)
215218
0 commit comments