Skip to content

Commit 626b270

Browse files
committed
update: change Context to be Infoless - to get the right sort of equality
1 parent 9b44f13 commit 626b270

File tree

5 files changed

+41
-55
lines changed

5 files changed

+41
-55
lines changed

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

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import Control.Monad (void)
1414
import Control.Monad.Freer (Eff, Member, Members, interpret, reinterpret, run)
1515
import Control.Monad.Freer.Error (Error, runError, throwError)
1616
import Control.Monad.Freer.Reader (Reader, ask, runReader)
17-
import Control.Monad.Freer.State (State, evalState)
1817
import Control.Monad.Freer.TH (makeEffect)
18+
import Data.Default (Default (def))
1919
import Data.Foldable (Foldable (toList), traverse_)
2020
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)
2424
import LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef, TyVar))
25+
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess, mkInfoLess)
2526
import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
2627

2728
--------------------------------------------------------------------------------
@@ -102,7 +103,7 @@ globalStrategy = reinterpret $ \case
102103

103104
moduleStrategy :: Transform GlobalCheck ModuleCheck
104105
moduleStrategy = reinterpret $ \case
105-
CreateContext ci -> evalState (mempty @(M.Map Variable PC.TyDef)) . resolveCreateContext $ ci
106+
CreateContext ci -> resolveCreateContext ci
106107
ValidateModule cx md -> do
107108
traverse_ (kCTypeDefinition (md ^. #moduleName) cx) (md ^. #typeDefs)
108109

@@ -117,9 +118,9 @@ runKindCheck :: forall effs {a}. Member Err effs => Eff (KindCheck ': effs) a ->
117118
runKindCheck = interpret $ \case
118119
-- TypesFromTyDef modName tydef -> runReader modName (tyDef2Types tydef)
119120
InferTypeKind modName tyDef ctx k -> either (handleErr modName tyDef) pure $ I.infer ctx tyDef k modName
120-
CheckKindConsistency modName def ctx k -> runReader modName $ resolveKindConsistency def ctx k
121+
CheckKindConsistency modName tydef ctx k -> runReader modName $ resolveKindConsistency tydef ctx k
121122
GetSpecifiedKind modName tyDef -> do
122-
(_, k) <- tyDef2NameAndKind modName tyDef
123+
(_, k) <- runReader modName $ tyDef2NameAndKind tyDef
123124
pure k
124125
where
125126
handleErr :: forall {b}. PC.ModuleName -> PC.TyDef -> I.InferErr -> Eff effs b
@@ -150,7 +151,7 @@ resolveKindConsistency ::
150151
Eff effs Kind
151152
resolveKindConsistency tydef _ctx inferredKind = do
152153
modname <- ask @PC.ModuleName
153-
(_, k) <- tyDef2NameAndKind modname tydef
154+
(_, k) <- tyDef2NameAndKind tydef
154155
guard tydef k inferredKind modname
155156
pure inferredKind
156157
where
@@ -165,64 +166,38 @@ resolveKindConsistency tydef _ctx inferredKind = do
165166
--------------------------------------------------------------------------------
166167
-- Context Creation
167168

168-
{- | Resolver function for the context creation - it fails if two identical
169-
declarations are found.
170-
-}
171-
resolveCreateContext ::
172-
forall effs.
173-
Member (State (M.Map Variable PC.TyDef)) effs =>
174-
Member Err effs =>
175-
PC.CompilerInput ->
176-
Eff effs Context
169+
-- | Resolver function for the context creation. There is a guarantee from ProtoCompat that the input is sanitised.
170+
resolveCreateContext :: forall effs. PC.CompilerInput -> Eff effs Context
177171
resolveCreateContext ci =
178172
mconcat <$> traverse module2Context (toList $ ci ^. #modules)
179173

180-
module2Context ::
181-
forall effs.
182-
Member (State (M.Map Variable PC.TyDef)) effs =>
183-
Member Err effs =>
184-
PC.Module ->
185-
Eff effs Context
174+
module2Context :: forall effs. PC.Module -> Eff effs Context
186175
module2Context m = do
187176
let typeDefinitions = toList $ m ^. #typeDefs
188-
ctxs <-
189-
runReader (m ^. #moduleName) $
190-
traverse tyDef2Context typeDefinitions
177+
ctxs <- runReader (m ^. #moduleName) $ traverse tyDef2Context typeDefinitions
191178
pure $ mconcat ctxs
192179

193180
-- | Creates a Context entry from one type definition.
194181
tyDef2Context ::
195182
forall effs.
196183
Member (Reader PC.ModuleName) effs =>
197-
Member Err effs =>
198184
PC.TyDef ->
199185
Eff effs Context
200186
tyDef2Context tyDef = do
201-
curModName <- ask @PC.ModuleName
202-
r <- tyDef2NameAndKind curModName tyDef
187+
r <- tyDef2NameAndKind tyDef
203188
pure $ mempty & context .~ uncurry M.singleton r
204189

205-
{-
206-
{- | Gets the kind of the variables from the definition and adds them to the
207-
context.
208-
-}
209-
tyDefArgs2Context :: PC.TyDef -> Eff effs (M.Map Variable Kind)
210-
tyDefArgs2Context tydef = do
211-
let ds = g <$> M.elems (tydef ^. #tyAbs . #tyArgs)
212-
pure $ M.fromList ds
213-
where
214-
g :: PC.TyArg -> (Variable, Kind)
215-
g tyarg = (v, k)
216-
where
217-
v = TyVar (tyarg ^. #argName)
218-
k = pKind2Kind (tyarg ^. #argKind)
219-
-}
220-
221-
tyDef2NameAndKind :: forall effs. PC.ModuleName -> PC.TyDef -> Eff effs (Variable, Kind)
222-
tyDef2NameAndKind curModName tyDef = do
223-
let name = ForeignRef $ view (PC.localRef2ForeignRef curModName) $ PC.LocalRef (tyDef ^. #tyName) (tyDef ^. #sourceInfo)
190+
tyDef2NameAndKind ::
191+
forall effs.
192+
Member (Reader PC.ModuleName) effs =>
193+
PC.TyDef ->
194+
Eff effs (InfoLess Variable, Kind)
195+
tyDef2NameAndKind tyDef = do
196+
curModName <- ask
197+
let tyname = tyDef ^. #tyName
198+
let name = ForeignRef $ view (PC.localRef2ForeignRef curModName) $ PC.LocalRef tyname def
224199
let k = tyAbsLHS2Kind (tyDef ^. #tyAbs)
225-
pure (name, k)
200+
pure (mkInfoLess name, k)
226201

227202
tyAbsLHS2Kind :: PC.TyAbs -> Kind
228203
tyAbsLHS2Kind tyAbs = foldWithArrowToType $ pKind2Kind . (\x -> x ^. #argKind) <$> toList (tyAbs ^. #tyArgs)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Control.Lens (makeLenses, (^.))
44
import Data.Map qualified as M
55
import LambdaBuffers.Compiler.KindCheck.Kind (Kind)
66
import LambdaBuffers.Compiler.KindCheck.Variable (Variable)
7+
import LambdaBuffers.Compiler.ProtoCompat (InfoLess)
78
import Prettyprinter (
89
Doc,
910
Pretty (pretty),
@@ -15,8 +16,8 @@ import Prettyprinter (
1516
)
1617

1718
data Context = Context
18-
{ _context :: M.Map Variable Kind
19-
, _addContext :: M.Map Variable Kind
19+
{ _context :: M.Map (InfoLess Variable) Kind
20+
, _addContext :: M.Map (InfoLess Variable) Kind
2021
}
2122
deriving stock (Show, Eq)
2223

@@ -27,7 +28,7 @@ instance Pretty Context where
2728
[] -> "Γ"
2829
ctx -> "Γ" <+> "" <+> braces (setPretty ctx)
2930
where
30-
setPretty :: [(Variable, Kind)] -> Doc ann
31+
setPretty :: [(InfoLess Variable, Kind)] -> Doc ann
3132
setPretty = hsep . punctuate comma . fmap (\(v, t) -> pretty v <> ":" <+> pretty t)
3233

3334
instance Semigroup Context where
@@ -37,5 +38,5 @@ instance Monoid Context where
3738
mempty = Context mempty mempty
3839

3940
-- | Utility to unify the two.
40-
getAllContext :: Context -> M.Map Variable Kind
41+
getAllContext :: Context -> M.Map (InfoLess Variable) Kind
4142
getAllContext c = c ^. context <> c ^. addContext

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ import Control.Monad.Freer.Error (Error, runError, throwError)
3131
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)
34+
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (mkInfoLess)
3435

3536
import LambdaBuffers.Compiler.ProtoCompat qualified as PC
3637

3738
import Data.String (fromString)
3839
import Data.Text qualified as T
3940

40-
import Control.Lens ((%~), (&), (.~), (^.))
41+
import Control.Lens (view, (%~), (&), (.~), (^.))
4142
import Data.Map qualified as M
4243

4344
import LambdaBuffers.Compiler.ProtoCompat (localRef2ForeignRef)
@@ -144,7 +145,7 @@ derive x = deriveTyAbs x
144145
freshT <- KVar <$> fresh
145146
ctx <- ask
146147

147-
let newContext = ctx & addContext %~ (<> M.singleton (TyVar n) vK)
148+
let newContext = ctx & addContext %~ (<> M.singleton (mkInfoLess (TyVar n)) vK)
148149
let newAbs = tyabs & #tyArgs .~ uncurry M.singleton a
149150
let restAbs = tyabs & #tyArgs .~ M.fromList as
150151

@@ -206,7 +207,7 @@ derive x = deriveTyAbs x
206207
deriveTyRef = \case
207208
PC.LocalI r -> do
208209
localModule <- ask
209-
let ty = ForeignRef $ r ^. localRef2ForeignRef localModule
210+
let ty = ForeignRef . view (localRef2ForeignRef localModule) $ r
210211
v <- getBinding ty
211212
c <- ask
212213
pure . Axiom . Judgement $ (c, Var ty, v)
@@ -273,7 +274,7 @@ derive x = deriveTyAbs x
273274
getBinding :: Variable -> Derive Kind
274275
getBinding t = do
275276
ctx <- asks getAllContext
276-
case ctx M.!? t of
277+
case ctx M.!? mkInfoLess t of
277278
Just x -> pure x
278279
Nothing -> throwError $ InferUnboundTermErr t
279280

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef, TyVar),
22

33
import Data.Text (Text)
44
import GHC.Generics (Generic)
5+
import Generics.SOP qualified as SOP
56
import LambdaBuffers.Compiler.ProtoCompat qualified as PC
7+
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess, InfoLessC, withInfoLess)
68
import Prettyprinter (Pretty (pretty), viaShow)
79
import Test.QuickCheck (Arbitrary)
810
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary (GenericArbitrary))
@@ -17,9 +19,15 @@ data Variable
1719
| TyVar PC.VarName
1820
deriving stock (Eq, Ord, Show, Generic)
1921
deriving (Arbitrary) via GenericArbitrary Variable
22+
deriving anyclass (SOP.Generic)
2023

2124
instance Pretty Variable where
2225
pretty = viaShow
2326

27+
instance InfoLessC Variable
28+
29+
instance Pretty (InfoLess Variable) where
30+
pretty x = withInfoLess x pretty
31+
2432
-- LocalRef a -> pretty a
2533
-- ForeignRef ms a -> concatWith (\x y -> x <> "." <> y) (pretty <$> (ms <> [a]))

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module LambdaBuffers.Compiler.ProtoCompat.InfoLess (
77
withInfoLess,
88
withInfoLessF,
99
mkInfoLess,
10+
InfoLessC,
1011
) where
1112

1213
import Data.Bifunctor (Bifunctor (bimap))
@@ -67,7 +68,7 @@ newtype InfoLess a = InfoLess {unsafeInfoLess :: a}
6768
deriving stock (Functor, Traversable, Foldable)
6869

6970
{- | SourceInfo Less ID.
70-
A TypeClass that provides id for types with SourceInfo - where SI is defaulted - therefore ignored. Not exported for obvious unsafe reasons.
71+
A TypeClass that provides id for types with SourceInfo - where SI is defaulted - therefore ignored. Can only be derived.
7172
-}
7273
class Eq a => InfoLessC a where
7374
silId :: a -> a

0 commit comments

Comments
 (0)