@@ -12,7 +12,6 @@ module LambdaBuffers.Compiler.KindCheck.Inference (
1212 Context (.. ),
1313 Atom ,
1414 Type (.. ),
15- DeriveM ,
1615 DeriveEff ,
1716 InferErr (.. ),
1817 Constraint (.. ),
@@ -31,30 +30,33 @@ import Control.Monad.Freer.State (State, evalState, get, put)
3130import Control.Monad.Freer.Writer (Writer , runWriter , tell )
3231import Data.Foldable (foldrM , traverse_ )
3332import Data.Map qualified as M
33+ import Data.Text (Text )
3434import Data.Text qualified as T
3535import LambdaBuffers.Compiler.KindCheck.Derivation (
3636 Context (Context ),
3737 Derivation (Abstraction , Application , Axiom , Implication ),
3838 Judgement (Judgement ),
3939 addContext ,
40- context ,
4140 d'kind ,
4241 d'type ,
42+ getAllContext ,
4343 )
4444import LambdaBuffers.Compiler.KindCheck.Kind (Atom , Kind (KConstraint , KType , KVar , (:->:) ))
4545import LambdaBuffers.Compiler.KindCheck.Type (
4646 Type (Abs , App , Constructor , Opaque , Product , Sum , UnitT , Var , VoidT ),
47- Variable (QualifiedTyRef , TyVar ),
47+ Variable (QualifiedTyClassRef , QualifiedTyRef , TyVar ),
48+ fcrISOqtcr ,
4849 ftrISOqtr ,
50+ lcrISOftcr ,
51+ lcrISOqtcr ,
4952 ltrISOqtr ,
5053 )
51- import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess , mkInfoLess )
54+ import LambdaBuffers.Compiler.ProtoCompat.InfoLess (mkInfoLess )
5255import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
5356import Prettyprinter (Pretty (pretty ), (<+>) )
5457
55- -- | Utility to unify the two.
56- getAllContext :: Context -> M. Map (InfoLess Variable ) Kind
57- getAllContext c = c ^. context <> c ^. addContext
58+ --------------------------------------------------------------------------------
59+ -- Types
5860
5961data InferErr
6062 = InferUnboundTermErr Variable
@@ -74,30 +76,20 @@ newtype Substitution = Substitution {getSubstitution :: (Atom, Kind)}
7476instance Pretty Substitution where
7577 pretty (Substitution (a, k)) = pretty a <+> " ↦" <+> pretty k
7678
77- newtype DerivationContext = DC
78- { _startAtom :: Atom
79- }
79+ --------------------------------------------------------------------------------
80+ -- Effects
8081
81- type DeriveEff = '[ State Context , State DerivationContext , State [ Constraint ], Error InferErr ]
82+ newtype DerivationContext = DC { _startAtom :: Atom }
8283
83- type DeriveM a = Eff DeriveEff a
84+ type DeriveEff =
85+ '[Reader Context , Reader PC. ModuleName , State DerivationContext , Writer [Constraint ], Error InferErr ]
8486
85- type Derive a =
86- forall effs .
87- Members
88- '[ Reader Context
89- , Reader PC. ModuleName
90- , State DerivationContext
91- , Writer [Constraint ]
92- , Error InferErr
93- ]
94- effs = >
95- Eff effs a
87+ type Derive a = forall effs . Members DeriveEff effs = > Eff effs a
9688
9789--------------------------------------------------------------------------------
9890-- Runners
9991
100- -- | Run derivation builder - not unified yet .
92+ -- | Run Derive Monad - not unified.
10193runDerive :: Context -> PC. ModuleName -> Derive a -> Either InferErr (a , [Constraint ])
10294runDerive ctx localMod =
10395 run . runError . runWriter . evalState (DC startAtom) . runReader ctx . runReader localMod
@@ -261,17 +253,24 @@ deriveClassDef :: PC.ClassDef -> Derive ()
261253deriveClassDef classDef = traverse_ deriveConstraint (classDef ^. # supers)
262254
263255deriveConstraint :: PC. Constraint -> Derive Derivation
264- deriveConstraint _constraint = do
265- -- ctx <- ask
266- -- FIXME
267- -- k2 <- getKind (ConstraintT undefined)
268- -- argD <- deriveTy (constraint ^. #argument)
269- pure $ error " NOTE(cstml): fixme."
270-
271- -- Application
272- -- (Judgement ctx (App (Var (QualifiedConstraint undefined))) k2)
273- -- (Judgement ctx (App (Var (QualifiedConstraint undefined) k2)))
274- -- argD
256+ deriveConstraint constraint = do
257+ mn <- ask
258+ ctx <- ask
259+ let qcr = case constraint ^. # classRef of
260+ PC. LocalCI lcr -> QualifiedTyClassRef . withIso lcrISOqtcr const $ (lcr, mn)
261+ PC. ForeignCI fcr -> QualifiedTyClassRef . withIso fcrISOqtcr const $ fcr
262+ dConstraint <- deriveVar qcr
263+ argD <- deriveTy (constraint ^. # argument)
264+ let argTy = argD ^. d'type
265+ freshK <- KVar <$> fresh
266+ tell [Constraint (dConstraint ^. d'kind, (argD ^. d'kind) :->: freshK)]
267+ pure $ Application (Judgement ctx (App (dConstraint ^. d'type) argTy) freshK) dConstraint argD
268+
269+ deriveVar :: Variable -> Derive Derivation
270+ deriveVar v = do
271+ ctx <- ask
272+ k <- getKind v
273+ pure . Axiom $ Judgement ctx (Var v) k
275274
276275--------------------------------------------------------------------------------
277276--
@@ -348,19 +347,13 @@ unify (constraint@(Constraint (l, r)) : xs) = case l of
348347
349348 appearsErr :: forall eff a . Member (Error InferErr ) eff => Atom -> Kind -> Eff eff a
350349 appearsErr var ty =
351- throwError $
352- InferRecursiveSubstitutionErr $
353- mconcat
354- [ " Cannot unify: "
355- , T. pack . show . pretty $ var
356- , " with "
357- , T. pack . show . pretty $ ty
358- , " . "
359- , T. pack . show . pretty $ var
360- , " appears in: "
361- , T. pack . show . pretty $ ty
362- , " ."
363- ]
350+ throwError
351+ $ InferRecursiveSubstitutionErr
352+ . mconcat
353+ $ [" Cannot unify: " , p var, " with " , p ty, " . " , p var, " appears in: " , p ty, " ." ]
354+ where
355+ p :: forall b . Pretty b => b -> Text
356+ p = T. pack . show . pretty
364357
365358 appearsIn a ty = a `elem` getVariables ty
366359
@@ -393,8 +386,6 @@ substitute s d = case d of
393386
394387 applySubstitutionCtx subs ctx = ctx & addContext %~ fmap (applySubstitution subs)
395388
396- -- FIXME(cstml) not avoiding any clashes
397-
398389-- | Fresh startAtom
399390startAtom :: Atom
400391startAtom = 0
@@ -405,5 +396,5 @@ protoKind2Kind = \case
405396 PC. Kind k -> case k of
406397 PC. KindArrow k1 k2 -> protoKind2Kind k1 :->: protoKind2Kind k2
407398 PC. KindRef PC. KType -> KType
408- PC. KindRef PC. KUnspecified -> KType -- unspecified kinds get defaulted
399+ PC. KindRef PC. KUnspecified -> KType
409400 PC. KindRef PC. KConstraint -> KConstraint
0 commit comments