11{-# OPTIONS_GHC -Wno-redundant-constraints #-}
22
33module LambdaBuffers.Compiler.KindCheck (
4- -- * Kindchecking functions.
4+ -- * Kind checking functions.
55 check ,
66 check_ ,
77
8- -- * Testing functions
8+ -- * Tested functions
99 foldWithArrowToType ,
1010) where
1111
@@ -64,16 +64,12 @@ data KindCheck a where
6464makeEffect ''KindCheck
6565
6666--------------------------------------------------------------------------------
67+ -- Runners
6768
6869-- | The Check effect runner.
6970runCheck :: Eff '[Check , Err ] a -> Either CompilerErr a
7071runCheck =
71- run
72- . runError
73- . runKindCheck
74- . localStrategy
75- . moduleStrategy
76- . globalStrategy
72+ run . runError . runKindCheck . localStrategy . moduleStrategy . globalStrategy
7773
7874{- | Run the check - return the validated context or the failure. The main API
7975 function of the library.
@@ -86,12 +82,14 @@ check_ :: PC.CompilerInput -> Either CompilerErr ()
8682check_ = void . runCheck . kCheck
8783
8884--------------------------------------------------------------------------------
85+ -- Transformations
8986
9087{- | A transformation (in the context of the Kind Checker) is a mapping from one
91- Effect to another. All effects can fial via the `Err` effect - which is
88+ Effect to another. All effects can fail via the `Err` effect - which is
9289 essentially the Kind Check failure.
9390-}
94- type Transform x y = forall effs {a }. Member Err effs = > Eff (x ': effs ) a -> Eff (y ': effs ) a
91+ type Transform x y =
92+ forall effs {a }. Member Err effs = > Eff (x ': effs ) a -> Eff (y ': effs ) a
9593
9694-- Transformation strategies
9795globalStrategy :: Transform Check GlobalCheck
@@ -103,25 +101,26 @@ globalStrategy = reinterpret $ \case
103101
104102moduleStrategy :: Transform GlobalCheck ModuleCheck
105103moduleStrategy = reinterpret $ \ case
106- CreateContext ci -> resolveCreateContext ci
107- ValidateModule cx md -> do
104+ CreateContext ci ->
105+ resolveCreateContext ci
106+ ValidateModule cx md ->
108107 traverse_ (kCTypeDefinition (md ^. # moduleName) cx) (md ^. # typeDefs)
109108
110109localStrategy :: Transform ModuleCheck KindCheck
111110localStrategy = reinterpret $ \ case
112- KCTypeDefinition mname ctx tydef -> do
113- desiredK <- getSpecifiedKind mname tydef
114- k <- inferTypeKind mname tydef ctx desiredK
115- checkKindConsistency mname tydef ctx k
111+ KCTypeDefinition modName ctx tyDef -> do
112+ desiredK <- getSpecifiedKind modName tyDef
113+ k <- inferTypeKind modName tyDef ctx desiredK
114+ checkKindConsistency modName tyDef ctx k
116115
117116runKindCheck :: forall effs {a }. Member Err effs => Eff (KindCheck ': effs ) a -> Eff effs a
118117runKindCheck = interpret $ \ case
119- -- TypesFromTyDef modName tydef -> runReader modName (tyDef2Types tydef)
120- InferTypeKind modName tyDef ctx k -> either (handleErr modName tyDef) pure $ I. infer ctx tyDef k modName
121- CheckKindConsistency modName tydef ctx k -> runReader modName $ resolveKindConsistency tydef ctx k
122- GetSpecifiedKind modName tyDef -> do
123- (_, k) <- runReader modName $ tyDef2NameAndKind tyDef
124- pure k
118+ InferTypeKind modName tyDef ctx k ->
119+ either (handleErr modName tyDef) pure $ I. infer ctx tyDef k modName
120+ CheckKindConsistency modName tyDef ctx k ->
121+ runReader modName $ resolveKindConsistency tyDef ctx k
122+ GetSpecifiedKind modName tyDef ->
123+ fmap snd $ runReader modName $ tyDef2NameAndKind tyDef
125124 where
126125 handleErr :: forall {b }. PC. ModuleName -> PC. TyDef -> I. InferErr -> Eff effs b
127126 handleErr modName td = \ case
@@ -130,17 +129,32 @@ runKindCheck = interpret $ \case
130129 ForeignRef fr ->
131130 if (fr ^. # moduleName) == modName
132131 then -- We're looking at the local module.
133- throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. LocalI $ fr ^. PC. foreignRef2LocalRef) modName
132+
133+ throwError
134+ . PC. CompKindCheckError
135+ $ PC. UnboundTyRefError td (PC. LocalI $ fr ^. PC. foreignRef2LocalRef) modName
134136 else -- We're looking at a foreign module.
135- throwError . PC. CompKindCheckError $ PC. UnboundTyRefError td (PC. ForeignI fr) modName
136- TyVar tv -> throwError . PC. CompKindCheckError $ PC. UnboundTyVarError td (PC. TyVar tv) modName
137+
138+ throwError
139+ . PC. CompKindCheckError
140+ $ PC. UnboundTyRefError td (PC. ForeignI fr) modName
141+ TyVar tv ->
142+ throwError
143+ . PC. CompKindCheckError
144+ $ PC. UnboundTyVarError td (PC. TyVar tv) modName
137145 I. InferUnifyTermErr (I. Constraint (k1, k2)) ->
138- throwError . PC. CompKindCheckError $ PC. IncorrectApplicationError td (kind2ProtoKind k1) (kind2ProtoKind k2) modName
146+ throwError
147+ . PC. CompKindCheckError
148+ $ PC. IncorrectApplicationError td (kind2ProtoKind k1) (kind2ProtoKind k2) modName
139149 I. InferRecursiveSubstitutionErr _ ->
140- throwError . PC. CompKindCheckError $ PC. RecursiveKindError td modName
150+ throwError
151+ . PC. CompKindCheckError
152+ $ PC. RecursiveKindError td modName
141153 I. InferImpossibleErr t ->
142- throwError . PC. InternalError $ t
154+ throwError $
155+ PC. InternalError t
143156
157+ --------------------------------------------------------------------------------
144158-- Resolvers
145159resolveKindConsistency ::
146160 forall effs .
@@ -149,16 +163,16 @@ resolveKindConsistency ::
149163 Context ->
150164 Kind ->
151165 Eff effs Kind
152- resolveKindConsistency tydef _ctx inferredKind = do
153- modname <- ask @ PC. ModuleName
154- (_, k) <- tyDef2NameAndKind tydef
155- guard tydef k inferredKind modname
166+ resolveKindConsistency tyDef _ctx inferredKind = do
167+ (_, k) <- tyDef2NameAndKind tyDef
168+ guard tyDef k inferredKind
156169 pure inferredKind
157170 where
158- guard :: PC. TyDef -> Kind -> Kind -> PC. ModuleName -> Eff effs ()
159- guard t i d mn
171+ guard :: PC. TyDef -> Kind -> Kind -> Eff effs ()
172+ guard t i d
160173 | i == d = pure ()
161- | otherwise =
174+ | otherwise = do
175+ mn <- ask
162176 throwError
163177 . PC. CompKindCheckError
164178 $ PC. InconsistentTypeError t (kind2ProtoKind i) (kind2ProtoKind d) mn
@@ -194,9 +208,15 @@ tyDef2NameAndKind ::
194208 Eff effs (InfoLess Variable , Kind )
195209tyDef2NameAndKind tyDef = do
196210 curModName <- ask
197- let tyname = tyDef ^. # tyName
198- let name = ForeignRef $ view (PC. localRef2ForeignRef curModName) $ PC. LocalRef tyname def
199- let k = tyAbsLHS2Kind (tyDef ^. # tyAbs)
211+
212+ -- InfoLess name - the SourceInfo doesn't matter therefore it is defaulted.
213+ let name =
214+ ForeignRef
215+ . view (PC. localRef2ForeignRef curModName)
216+ $ PC. LocalRef (tyDef ^. # tyName) def
217+
218+ k = tyAbsLHS2Kind (tyDef ^. # tyAbs)
219+
200220 pure (mkInfoLess name, k)
201221
202222tyAbsLHS2Kind :: PC. TyAbs -> Kind
@@ -220,13 +240,12 @@ foldWithArrowToType = foldWithArrowToKind KType
220240foldWithArrowToKind :: Kind -> [Kind ] -> Kind
221241foldWithArrowToKind = foldr (:->:)
222242
223- -- =============================================================================
243+ --------------------------------------------------------------------------------
224244-- To Kind Conversion functions
225245
226246pKind2Kind :: PC. Kind -> Kind
227247pKind2Kind k =
228248 case k ^. # kind of
229249 PC. KindRef PC. KType -> KType
230250 PC. KindArrow l r -> pKind2Kind l :->: pKind2Kind r
231- -- NOTE(cstml): What is an undefined Kind type meant to mean?
232- _ -> error " Fixme undefined type"
251+ PC. KindRef PC. KUnspecified -> KType -- (for now) defaulting unspecified kinds to Type
0 commit comments