Skip to content

Commit 9b44f13

Browse files
committed
update: change how variables find their kind
1 parent ec2960a commit 9b44f13

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lambda-buffers-compiler/src/LambdaBuffers/Compiler/KindCheck.hs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ makeEffect ''KindCheck
6666

6767
-- | The Check effect runner.
6868
runCheck :: Eff '[Check, Err] a -> Either CompilerErr a
69-
runCheck = run . runError . runKindCheck . localStrategy . moduleStrategy . globalStrategy
69+
runCheck =
70+
run
71+
. runError
72+
. runKindCheck
73+
. localStrategy
74+
. moduleStrategy
75+
. globalStrategy
7076

7177
{- | Run the check - return the validated context or the failure. The main API
7278
function of the library.
@@ -168,9 +174,8 @@ resolveCreateContext ::
168174
Member Err effs =>
169175
PC.CompilerInput ->
170176
Eff effs Context
171-
resolveCreateContext ci = do
172-
ctxs <- traverse module2Context (toList $ ci ^. #modules)
173-
pure $ mconcat ctxs
177+
resolveCreateContext ci =
178+
mconcat <$> traverse module2Context (toList $ ci ^. #modules)
174179

175180
module2Context ::
176181
forall effs.
@@ -180,7 +185,9 @@ module2Context ::
180185
Eff effs Context
181186
module2Context m = do
182187
let typeDefinitions = toList $ m ^. #typeDefs
183-
ctxs <- runReader (m ^. #moduleName) $ traverse tyDef2Context typeDefinitions
188+
ctxs <-
189+
runReader (m ^. #moduleName) $
190+
traverse tyDef2Context typeDefinitions
184191
pure $ mconcat ctxs
185192

186193
-- | Creates a Context entry from one type definition.
@@ -193,9 +200,9 @@ tyDef2Context ::
193200
tyDef2Context tyDef = do
194201
curModName <- ask @PC.ModuleName
195202
r <- tyDef2NameAndKind curModName tyDef
196-
ctx2 <- tyDefArgs2Context tyDef
197-
pure $ mempty & context .~ uncurry M.singleton r <> ctx2
203+
pure $ mempty & context .~ uncurry M.singleton r
198204

205+
{-
199206
{- | Gets the kind of the variables from the definition and adds them to the
200207
context.
201208
-}
@@ -209,6 +216,7 @@ tyDefArgs2Context tydef = do
209216
where
210217
v = TyVar (tyarg ^. #argName)
211218
k = pKind2Kind (tyarg ^. #argKind)
219+
-}
212220

213221
tyDef2NameAndKind :: forall effs. PC.ModuleName -> PC.TyDef -> Eff effs (Variable, Kind)
214222
tyDef2NameAndKind curModName tyDef = do

lambda-buffers-compiler/src/LambdaBuffers/Compiler/KindCheck/Inference.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import Data.Foldable (foldrM)
2222
import LambdaBuffers.Compiler.KindCheck.Context (Context (Context), addContext, context, getAllContext)
2323
import LambdaBuffers.Compiler.KindCheck.Derivation (Derivation (Abstraction, Application, Axiom, Implication), dTopKind, dType)
2424
import 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)
2626
import LambdaBuffers.Compiler.KindCheck.Type (Type (Abs, App, Constructor, Opaque, Product, Sum, Var, VoidT))
2727
import LambdaBuffers.Compiler.KindCheck.Variable (Atom, Variable (ForeignRef, TyVar))
2828

2929
import Control.Monad.Freer (Eff, Member, Members, run)
3030
import 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)
3232
import Control.Monad.Freer.State (State, evalState, get, put)
3333
import Control.Monad.Freer.Writer (Writer, runWriter, tell)
3434

@@ -37,7 +37,7 @@ import LambdaBuffers.Compiler.ProtoCompat qualified as PC
3737
import Data.String (fromString)
3838
import Data.Text qualified as T
3939

40-
import Control.Lens ((&), (.~), (^.))
40+
import Control.Lens ((%~), (&), (.~), (^.))
4141
import Data.Map qualified as M
4242

4343
import 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

Comments
 (0)