@@ -14,14 +14,15 @@ import Control.Monad (void)
1414import Control.Monad.Freer (Eff , Member , Members , interpret , reinterpret , run )
1515import Control.Monad.Freer.Error (Error , runError , throwError )
1616import Control.Monad.Freer.Reader (Reader , ask , runReader )
17- import Control.Monad.Freer.State (State , evalState )
1817import Control.Monad.Freer.TH (makeEffect )
18+ import Data.Default (Default (def ))
1919import Data.Foldable (Foldable (toList ), traverse_ )
2020import 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 )
2424import LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef , TyVar ))
25+ import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess , mkInfoLess )
2526import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
2627
2728--------------------------------------------------------------------------------
@@ -102,7 +103,7 @@ globalStrategy = reinterpret $ \case
102103
103104moduleStrategy :: Transform GlobalCheck ModuleCheck
104105moduleStrategy = 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 ->
117118runKindCheck = 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
151152resolveKindConsistency 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
177171resolveCreateContext 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
186175module2Context 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.
194181tyDef2Context ::
195182 forall effs .
196183 Member (Reader PC. ModuleName ) effs =>
197- Member Err effs =>
198184 PC. TyDef ->
199185 Eff effs Context
200186tyDef2Context 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
227202tyAbsLHS2Kind :: PC. TyAbs -> Kind
228203tyAbsLHS2Kind tyAbs = foldWithArrowToType $ pKind2Kind . (\ x -> x ^. # argKind) <$> toList (tyAbs ^. # tyArgs)
0 commit comments