Skip to content

Commit ec2960a

Browse files
committed
fix: all refs are qualified
1 parent b9c37b0 commit ec2960a

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module LambdaBuffers.Compiler.KindCheck (
99
foldWithArrowToType,
1010
) where
1111

12-
import Control.Lens ((&), (.~), (^.))
12+
import Control.Lens (view, (&), (.~), (^.))
1313
import Control.Monad (void)
1414
import Control.Monad.Freer (Eff, Member, Members, interpret, reinterpret, run)
1515
import Control.Monad.Freer.Error (Error, runError, throwError)
@@ -21,7 +21,7 @@ import Data.Map qualified as M
2121
import LambdaBuffers.Compiler.KindCheck.Context (Context, context)
2222
import LambdaBuffers.Compiler.KindCheck.Inference qualified as I
2323
import 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))
2525
import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
2626

2727
--------------------------------------------------------------------------------
@@ -110,7 +110,7 @@ localStrategy = reinterpret $ \case
110110
runKindCheck :: forall effs {a}. Member Err effs => Eff (KindCheck ': effs) a -> Eff effs a
111111
runKindCheck = 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

209213
tyDef2NameAndKind :: 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

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import LambdaBuffers.Compiler.KindCheck.Derivation (Derivation (Abstraction, App
2424
import LambdaBuffers.Compiler.KindCheck.Judgement (Judgement (Judgement))
2525
import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KType, KVar, (:->:)))
2626
import LambdaBuffers.Compiler.KindCheck.Type (Type (Abs, App, Constructor, Opaque, Product, Sum, Var, VoidT))
27-
import LambdaBuffers.Compiler.KindCheck.Variable (Atom, Variable (ForeignRef, LocalRef, TyVar))
27+
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)
@@ -40,6 +40,7 @@ import Data.Text qualified as T
4040
import Control.Lens ((&), (.~), (^.))
4141
import Data.Map qualified as M
4242

43+
import LambdaBuffers.Compiler.ProtoCompat (localRef2ForeignRef)
4344
import Prettyprinter (
4445
Pretty (pretty),
4546
(<+>),
@@ -76,6 +77,7 @@ type Derive a =
7677
Members
7778
'[ Reader Context
7879
, Reader Kind
80+
, Reader PC.ModuleName
7981
, State DerivationContext
8082
, Writer [Constraint]
8183
, Error InferErr
@@ -87,12 +89,30 @@ type Derive a =
8789
-- Runners
8890

8991
-- | Run derivation builder - not unified yet.
90-
runDerive :: Context -> PC.TyAbs -> Kind -> Either InferErr (Derivation, [Constraint])
91-
runDerive ctx t k = run $ runError $ runWriter $ evalState (DC atoms) $ runReader ctx $ runReader k (derive t)
92-
93-
infer :: Context -> PC.TyDef -> Kind -> Either InferErr Kind
94-
infer ctx t k = do
95-
(d, c) <- runDerive (defContext <> ctx) (t ^. #tyAbs) k
92+
runDerive ::
93+
Context ->
94+
PC.TyAbs ->
95+
Kind ->
96+
PC.ModuleName ->
97+
Either InferErr (Derivation, [Constraint])
98+
runDerive ctx t k localMod =
99+
run $
100+
runError $
101+
runWriter $
102+
evalState (DC atoms) $
103+
runReader ctx $
104+
runReader k $
105+
runReader localMod $
106+
derive t
107+
108+
infer ::
109+
Context ->
110+
PC.TyDef ->
111+
Kind ->
112+
PC.ModuleName ->
113+
Either InferErr Kind
114+
infer ctx t k localMod = do
115+
(d, c) <- runDerive (defContext <> ctx) (t ^. #tyAbs) k localMod
96116
s <- runUnify' c
97117
let res = foldl (flip substitute) d s
98118
pure $ res ^. dTopKind
@@ -178,7 +198,8 @@ derive x = deriveTyAbs x
178198
deriveTyRef :: PC.TyRef -> Derive Derivation
179199
deriveTyRef = \case
180200
PC.LocalI r -> do
181-
let ty = LocalRef r
201+
localModule <- ask
202+
let ty = ForeignRef $ r ^. localRef2ForeignRef localModule
182203
v <- getBinding ty
183204
c <- ask
184205
pure . Axiom . Judgement $ (c, Var ty, v)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module LambdaBuffers.Compiler.KindCheck.Variable (Variable (LocalRef, ForeignRef, TyVar), Atom) where
1+
module LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef, TyVar), Atom) where
22

33
import Data.Text (Text)
44
import GHC.Generics (Generic)
@@ -11,8 +11,9 @@ import Test.QuickCheck.Instances.Text ()
1111
type Atom = Text
1212

1313
data Variable
14-
= LocalRef PC.LocalRef
15-
| ForeignRef PC.ForeignRef
14+
= -- | Notionally all Refs. are fully qualified. The context determines if
15+
-- they're local or not.
16+
ForeignRef PC.ForeignRef
1617
| TyVar PC.VarName
1718
deriving stock (Eq, Ord, Show, Generic)
1819
deriving (Arbitrary) via GenericArbitrary Variable

lambda-buffers-compiler/src/LambdaBuffers/Compiler/ProtoCompat/Types.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}
77

88
module LambdaBuffers.Compiler.ProtoCompat.Types (
9+
foreignRef2LocalRef,
10+
localRef2ForeignRef,
911
ClassDef (..),
1012
ClassName (..),
1113
CompilerError (..),
@@ -53,6 +55,8 @@ module LambdaBuffers.Compiler.ProtoCompat.Types (
5355
) where
5456

5557
import Control.Exception (Exception)
58+
import Control.Lens (Getter, to, (^.))
59+
import Data.Generics.Labels ()
5660
import Data.Map (Map)
5761
import Data.Set (Set)
5862
import Data.Text (Text)
@@ -173,11 +177,32 @@ data ForeignRef = ForeignRef {tyName :: TyName, moduleName :: ModuleName, source
173177
deriving (Arbitrary) via GenericArbitrary ForeignRef
174178
deriving anyclass (SOP.Generic)
175179

180+
foreignRef2LocalRef :: Getter ForeignRef LocalRef
181+
foreignRef2LocalRef =
182+
to
183+
( \fr ->
184+
LocalRef
185+
{ tyName = fr ^. #tyName
186+
, sourceInfo = fr ^. #sourceInfo
187+
}
188+
)
189+
176190
data LocalRef = LocalRef {tyName :: TyName, sourceInfo :: SourceInfo}
177191
deriving stock (Show, Eq, Ord, Generic)
178192
deriving (Arbitrary) via GenericArbitrary LocalRef
179193
deriving anyclass (SOP.Generic)
180194

195+
localRef2ForeignRef :: ModuleName -> Getter LocalRef ForeignRef
196+
localRef2ForeignRef modName =
197+
to
198+
( \lr ->
199+
ForeignRef
200+
{ tyName = lr ^. #tyName
201+
, sourceInfo = lr ^. #sourceInfo
202+
, moduleName = modName
203+
}
204+
)
205+
181206
data TyRef = LocalI LocalRef | ForeignI ForeignRef
182207
deriving stock (Show, Eq, Ord, Generic)
183208
deriving (Arbitrary) via GenericArbitrary TyRef

0 commit comments

Comments
 (0)