11module 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
917import 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+
1121import 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+
2274data 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