Skip to content

Commit 7c3f00f

Browse files
committed
bugfix: Fix Constraint's kinds
1 parent f392aeb commit 7c3f00f

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import Control.Monad.Freer.TH (makeEffect)
1919
import Data.Default (Default (def))
2020
import Data.Foldable (Foldable (toList), traverse_)
2121
import Data.Map qualified as M
22-
import LambdaBuffers.Compiler.KindCheck.Derivation (Context, constraintContext, context)
22+
import LambdaBuffers.Compiler.KindCheck.Derivation (Context, classContext, context)
2323
import LambdaBuffers.Compiler.KindCheck.Inference (protoKind2Kind)
2424
import LambdaBuffers.Compiler.KindCheck.Inference qualified as I
2525
import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KConstraint, KType, KVar, (:->:)))
2626
import LambdaBuffers.Compiler.KindCheck.Type (
27+
QualifiedTyClassRefName (QualifiedTyClassRefName),
2728
Variable (QualifiedTyClassRef, QualifiedTyRef, TyVar),
2829
fcrISOqtcr,
2930
ftrISOqtr,
@@ -302,11 +303,13 @@ kind2ProtoKind = \case
302303
--------------------------------------------------------------------------------
303304
-- Class Definition Based Context Building.
304305

305-
classDef2Context :: forall effs. PC.ClassDef -> Eff effs Context
306+
classDef2Context :: forall effs. Member (Reader PC.ModuleName) effs => PC.ClassDef -> Eff effs Context
306307
classDef2Context cDef = do
307-
let className = mkInfoLess . view #className $ cDef
308+
modName <- ask
309+
let className = cDef ^. #className
310+
let qtcn = mkInfoLess . QualifiedTyClassRef $ QualifiedTyClassRefName className modName def
308311
let classArg = tyArg2Kind . view #classArgs $ cDef
309-
pure $ mempty & constraintContext .~ M.singleton className classArg
312+
pure $ mempty & classContext .~ M.singleton qtcn (classArg :->: KConstraint)
310313

311314
--------------------------------------------------------------------------------
312315
-- utilities

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# OPTIONS_GHC -Wno-unused-imports #-}
2+
13
module LambdaBuffers.Compiler.KindCheck.Derivation (
24
Derivation (Axiom, Abstraction, Application, Implication),
35
QClassName (QClassName),
@@ -11,15 +13,14 @@ module LambdaBuffers.Compiler.KindCheck.Derivation (
1113
context,
1214
addContext,
1315
getAllContext,
14-
constraintContext,
16+
classContext,
1517
) where
1618

1719
import Control.Lens (Lens', lens, makeLenses, (&), (.~), (^.))
1820
import Control.Lens.Operators ((%~))
1921
import Data.Map qualified as M
2022
import LambdaBuffers.Compiler.KindCheck.Kind (Kind)
21-
import LambdaBuffers.Compiler.KindCheck.Type (Type, Variable)
22-
import LambdaBuffers.Compiler.ProtoCompat qualified as PC
23+
import LambdaBuffers.Compiler.KindCheck.Type (Type, Variable (QualifiedTyClassRef))
2324
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess)
2425
import Prettyprinter (
2526
Doc,
@@ -43,7 +44,7 @@ data QClassName = QClassName
4344
data Context = Context
4445
{ _context :: M.Map (InfoLess Variable) Kind
4546
, _addContext :: M.Map (InfoLess Variable) Kind
46-
, _constraintContext :: M.Map (InfoLess PC.ClassName) Kind
47+
, _classContext :: M.Map (InfoLess Variable) Kind
4748
}
4849
deriving stock (Show, Eq)
4950

@@ -62,14 +63,14 @@ instance Semigroup Context where
6263
c1
6364
& context %~ (<> c2 ^. context)
6465
& addContext %~ (<> c2 ^. addContext)
65-
& constraintContext %~ (<> c2 ^. constraintContext)
66+
& classContext %~ (<> c2 ^. classContext)
6667

6768
instance Monoid Context where
6869
mempty = Context mempty mempty mempty
6970

7071
-- | Utility to unify the two T.
7172
getAllContext :: Context -> M.Map (InfoLess Variable) Kind
72-
getAllContext c = c ^. context <> c ^. addContext
73+
getAllContext c = c ^. context <> c ^. addContext <> c ^. classContext
7374

7475
data Judgement = Judgement
7576
{ _j'ctx :: Context

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module LambdaBuffers.Compiler.KindCheck.Inference (
2121
) where
2222

2323
import Control.Lens ((%~), (&), (.~), (^.))
24+
import Control.Lens.Combinators (to)
2425
import Control.Lens.Iso (withIso)
2526
import Control.Monad (void)
2627
import Control.Monad.Freer (Eff, Member, Members, run)
@@ -47,7 +48,6 @@ import LambdaBuffers.Compiler.KindCheck.Type (
4748
Variable (QualifiedTyClassRef, QualifiedTyRef, TyVar),
4849
fcrISOqtcr,
4950
ftrISOqtr,
50-
lcrISOftcr,
5151
lcrISOqtcr,
5252
ltrISOqtr,
5353
)
@@ -250,8 +250,17 @@ runClassDefCheck ctx modName classDef = do
250250

251251
-- | Checks the class definition for correct typedness.
252252
deriveClassDef :: PC.ClassDef -> Derive ()
253-
deriveClassDef classDef =
254-
traverse_ deriveConstraint (classDef ^. #supers)
253+
deriveClassDef classDef = do
254+
vars <- createLocalConstraintContext classDef
255+
traverse_ (local (<> vars) . deriveConstraint) (classDef ^. #supers)
256+
257+
-- | Adds the kind of the variable to the local context.
258+
createLocalConstraintContext :: PC.ClassDef -> Derive Context
259+
createLocalConstraintContext cd = do
260+
let arg = cd ^. #classArgs
261+
let n = mkInfoLess $ TyVar $ arg ^. #argName
262+
let k = arg ^. #argKind . to protoKind2Kind
263+
return $ mempty & addContext .~ M.singleton n k
255264

256265
deriveConstraint :: PC.Constraint -> Derive Derivation
257266
deriveConstraint constraint = do
@@ -397,5 +406,5 @@ protoKind2Kind = \case
397406
PC.Kind k -> case k of
398407
PC.KindArrow k1 k2 -> protoKind2Kind k1 :->: protoKind2Kind k2
399408
PC.KindRef PC.KType -> KType
400-
PC.KindRef PC.KUnspecified -> KType
401409
PC.KindRef PC.KConstraint -> KConstraint
410+
PC.KindRef PC.KUnspecified -> KType

lambda-buffers-compiler/test/Test/Utils/Module.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ module'classEq = _Module (_ModuleName ["Module"]) mempty [classDef'Eq] mempty
8282
class Eq a => Ord a.
8383
-}
8484
module'classOrd :: PC.Module
85-
module'classOrd = _Module (_ModuleName ["Module"]) mempty [classDef'Ord] mempty
85+
module'classOrd = _Module (_ModuleName ["Module"]) mempty [classDef'Eq, classDef'Ord] mempty

0 commit comments

Comments
 (0)