Skip to content

Commit bd625e1

Browse files
committed
update: comments
1 parent eda84be commit bd625e1

File tree

10 files changed

+218
-215
lines changed

10 files changed

+218
-215
lines changed

lambda-buffers-compiler/lambda-buffers-compiler.cabal

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,10 @@ library
111111
exposed-modules:
112112
LambdaBuffers.Compiler
113113
LambdaBuffers.Compiler.KindCheck
114-
LambdaBuffers.Compiler.KindCheck.Context
115114
LambdaBuffers.Compiler.KindCheck.Derivation
116115
LambdaBuffers.Compiler.KindCheck.Inference
117-
LambdaBuffers.Compiler.KindCheck.Judgement
118116
LambdaBuffers.Compiler.KindCheck.Kind
119117
LambdaBuffers.Compiler.KindCheck.Type
120-
LambdaBuffers.Compiler.KindCheck.Variable
121118
LambdaBuffers.Compiler.NamingCheck
122119
LambdaBuffers.Compiler.ProtoCompat
123120
LambdaBuffers.Compiler.ProtoCompat.FromProto

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

Lines changed: 17 additions & 6 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 (view, (&), (.~), (^.))
12+
import Control.Lens (Getter, to, 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)
@@ -18,10 +18,11 @@ import Control.Monad.Freer.TH (makeEffect)
1818
import Data.Default (Default (def))
1919
import Data.Foldable (Foldable (toList), traverse_)
2020
import Data.Map qualified as M
21-
import LambdaBuffers.Compiler.KindCheck.Context (Context, context)
21+
22+
import LambdaBuffers.Compiler.KindCheck.Derivation (Context, context)
2223
import LambdaBuffers.Compiler.KindCheck.Inference qualified as I
2324
import LambdaBuffers.Compiler.KindCheck.Kind (Kind (KType, (:->:)), kind2ProtoKind)
24-
import LambdaBuffers.Compiler.KindCheck.Variable (Variable (ForeignRef, TyVar))
25+
import LambdaBuffers.Compiler.KindCheck.Type (Variable (QualifiedTyRef, TyVar))
2526
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess, mkInfoLess)
2627
import LambdaBuffers.Compiler.ProtoCompat.Types qualified as PC
2728

@@ -126,13 +127,13 @@ runKindCheck = interpret $ \case
126127
handleErr modName td = \case
127128
I.InferUnboundTermErr uA -> do
128129
case uA of
129-
ForeignRef fr ->
130+
QualifiedTyRef fr ->
130131
if (fr ^. #moduleName) == modName
131132
then -- We're looking at the local module.
132133

133134
throwError
134135
. PC.CompKindCheckError
135-
$ PC.UnboundTyRefError td (PC.LocalI $ fr ^. PC.foreignRef2LocalRef) modName
136+
$ PC.UnboundTyRefError td (PC.LocalI $ fr ^. foreignRef2LocalRef) modName
136137
else -- We're looking at a foreign module.
137138

138139
throwError
@@ -154,6 +155,16 @@ runKindCheck = interpret $ \case
154155
throwError $
155156
PC.InternalError t
156157

158+
foreignRef2LocalRef :: Getter PC.ForeignRef PC.LocalRef
159+
foreignRef2LocalRef =
160+
to
161+
( \fr ->
162+
PC.LocalRef
163+
{ tyName = fr ^. #tyName
164+
, sourceInfo = fr ^. #sourceInfo
165+
}
166+
)
167+
157168
--------------------------------------------------------------------------------
158169
-- Resolvers
159170
resolveKindConsistency ::
@@ -211,7 +222,7 @@ tyDef2NameAndKind tyDef = do
211222

212223
-- InfoLess name - the SourceInfo doesn't matter therefore it is defaulted.
213224
let name =
214-
ForeignRef
225+
QualifiedTyRef
215226
. view (PC.localRef2ForeignRef curModName)
216227
$ PC.LocalRef (tyDef ^. #tyName) def
217228

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,76 @@
11
module LambdaBuffers.Compiler.KindCheck.Derivation (
22
Derivation (Axiom, Abstraction, Application, Implication),
3-
dType,
4-
dTopKind,
3+
d'type,
4+
d'kind,
5+
Judgement (Judgement),
6+
j'type,
7+
j'kind,
8+
j'ctx,
9+
Context (Context),
10+
context,
11+
addContext,
12+
getAllContext,
513
) where
614

7-
import Control.Lens (Lens', lens, (&), (.~), (^.))
8-
import LambdaBuffers.Compiler.KindCheck.Judgement (Judgement, jKind, jType)
15+
import Control.Lens (Lens', lens, makeLenses, (&), (.~), (^.))
16+
import Data.Map qualified as M
917
import LambdaBuffers.Compiler.KindCheck.Kind (Kind)
10-
import LambdaBuffers.Compiler.KindCheck.Type (Type)
18+
import LambdaBuffers.Compiler.KindCheck.Type (Type, Variable)
19+
import LambdaBuffers.Compiler.ProtoCompat.InfoLess (InfoLess)
20+
1121
import Prettyprinter (
1222
Doc,
1323
Pretty (pretty),
24+
braces,
25+
comma,
1426
encloseSep,
1527
hang,
28+
hsep,
1629
lbracket,
1730
line,
31+
punctuate,
1832
rbracket,
1933
space,
34+
(<+>),
2035
)
2136

37+
data Context = Context
38+
{ _context :: M.Map (InfoLess Variable) Kind
39+
, _addContext :: M.Map (InfoLess Variable) Kind
40+
}
41+
deriving stock (Show, Eq)
42+
43+
makeLenses ''Context
44+
45+
instance Pretty Context where
46+
pretty c = case M.toList (c ^. addContext) of
47+
[] -> "Γ"
48+
ctx -> "Γ" <+> "" <+> braces (setPretty ctx)
49+
where
50+
setPretty :: [(InfoLess Variable, Kind)] -> Doc ann
51+
setPretty = hsep . punctuate comma . fmap (\(v, t) -> pretty v <> ":" <+> pretty t)
52+
53+
instance Semigroup Context where
54+
(Context a1 b1) <> (Context a2 b2) = Context (a1 <> a2) (b1 <> b2)
55+
56+
instance Monoid Context where
57+
mempty = Context mempty mempty
58+
59+
-- | Utility to unify the two.
60+
getAllContext :: Context -> M.Map (InfoLess Variable) Kind
61+
getAllContext c = c ^. context <> c ^. addContext
62+
63+
data Judgement = Judgement
64+
{ _j'ctx :: Context
65+
, _j'type :: Type
66+
, _j'kind :: Kind
67+
}
68+
deriving stock (Show, Eq)
69+
makeLenses ''Judgement
70+
71+
instance Pretty Judgement where
72+
pretty j = pretty (j ^. j'ctx) <+> "" <+> pretty (j ^. j'type) <+> ":" <+> pretty (j ^. j'kind)
73+
2274
data Derivation
2375
= Axiom Judgement
2476
| Abstraction Judgement Derivation
@@ -36,32 +88,32 @@ instance Pretty Derivation where
3688
dNest :: forall a b c. (Pretty a, Pretty b) => a -> [b] -> Doc c
3789
dNest j ds = pretty j <> line <> hang 2 (encloseSep (lbracket <> space) rbracket (space <> "" <> space) (pretty <$> ds))
3890

39-
dType :: Lens' Derivation Type
40-
dType = lens from to
91+
d'type :: Lens' Derivation Type
92+
d'type = lens from to
4193
where
4294
from = \case
43-
Axiom j -> j ^. jType
44-
Abstraction j _ -> j ^. jType
45-
Application j _ _ -> j ^. jType
46-
Implication j _ -> j ^. jType
95+
Axiom j -> j ^. j'type
96+
Abstraction j _ -> j ^. j'type
97+
Application j _ _ -> j ^. j'type
98+
Implication j _ -> j ^. j'type
4799

48100
to drv t = case drv of
49-
Axiom j -> Axiom $ j & jType .~ t
50-
Abstraction j d -> Abstraction (j & jType .~ t) d
51-
Application j d1 d2 -> Application (j & jType .~ t) d1 d2
52-
Implication j d -> Implication (j & jType .~ t) d
101+
Axiom j -> Axiom $ j & j'type .~ t
102+
Abstraction j d -> Abstraction (j & j'type .~ t) d
103+
Application j d1 d2 -> Application (j & j'type .~ t) d1 d2
104+
Implication j d -> Implication (j & j'type .~ t) d
53105

54-
dTopKind :: Lens' Derivation Kind
55-
dTopKind = lens from to
106+
d'kind :: Lens' Derivation Kind
107+
d'kind = lens from to
56108
where
57109
from = \case
58-
Axiom j -> j ^. jKind
59-
Abstraction j _ -> j ^. jKind
60-
Application j _ _ -> j ^. jKind
61-
Implication j _ -> j ^. jKind
110+
Axiom j -> j ^. j'kind
111+
Abstraction j _ -> j ^. j'kind
112+
Application j _ _ -> j ^. j'kind
113+
Implication j _ -> j ^. j'kind
62114

63115
to der t = case der of
64-
Axiom j -> Axiom $ j & jKind .~ t
65-
Abstraction j d -> Abstraction (j & jKind .~ t) d
66-
Application j d1 d2 -> Application (j & jKind .~ t) d1 d2
67-
Implication j d -> Abstraction (j & jKind .~ t) d
116+
Axiom j -> Axiom $ j & j'kind .~ t
117+
Abstraction j d -> Abstraction (j & j'kind .~ t) d
118+
Application j d1 d2 -> Application (j & j'kind .~ t) d1 d2
119+
Implication j d -> Abstraction (j & j'kind .~ t) d

0 commit comments

Comments
 (0)