Skip to content

Commit cfab914

Browse files
authored
Merge pull request #1317 from google-research/delivering-the-decls
More prep for decls-in-binders
2 parents 6d23e46 + b274115 commit cfab914

35 files changed

+1118
-1371
lines changed

src/lib/AbstractSyntax.hs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,23 @@ topDecl = dropSrc topDecl' where
110110
topDecl' (CSDecl ann d) = ULocalDecl <$> decl ann (WithSrc emptySrcPosCtx d)
111111
topDecl' (CData name tyConParams givens constructors) = do
112112
tyConParams' <- aExplicitParams tyConParams
113-
givens' <- toNest <$> fromMaybeM givens [] aGivens
113+
givens' <- aOptGivens givens
114114
constructors' <- forM constructors \(v, ps) -> do
115115
ps' <- toNest <$> mapM tyOptBinder ps
116116
return (v, ps')
117117
return $ UDataDefDecl
118-
(UDataDef name (givens' >>> tyConParams') $
118+
(UDataDef name (catUOptAnnExplBinders givens' tyConParams') $
119119
map (\(name', cons) -> (name', UDataDefTrail cons)) constructors')
120120
(fromString name)
121121
(toNest $ map (fromString . fst) constructors')
122122
topDecl' (CStruct name params givens fields defs) = do
123123
params' <- aExplicitParams params
124-
givens' <- toNest <$> fromMaybeM givens [] aGivens
124+
givens' <- aOptGivens givens
125125
fields' <- forM fields \(v, ty) -> (v,) <$> expr ty
126126
methods <- forM defs \(ann, d) -> do
127127
(methodName, lam) <- aDef d
128128
return (ann, methodName, Abs (UBindSource emptySrcPosCtx "self") lam)
129-
return $ UStructDecl (fromString name) (UStructDef name (givens' >>> params') fields' methods)
129+
return $ UStructDecl (fromString name) (UStructDef name (catUOptAnnExplBinders givens' params') fields' methods)
130130
topDecl' (CInterface name params methods) = do
131131
params' <- aExplicitParams params
132132
(methodNames, methodTys) <- unzip <$> forM methods \(methodName, ty) -> do
@@ -153,7 +153,7 @@ aInstanceDef :: CInstanceDef -> SyntaxM (UTopDecl VoidS VoidS)
153153
aInstanceDef (CInstanceDef clName args givens methods instNameAndParams) = do
154154
let clName' = fromString clName
155155
args' <- mapM expr args
156-
givens' <- toNest <$> fromMaybeM givens [] aGivens
156+
givens' <- aOptGivens givens
157157
methods' <- catMaybes <$> mapM aMethod methods
158158
case instNameAndParams of
159159
Nothing -> return $ UInstance clName' givens' args' methods' NothingB ImplicitApp
@@ -162,7 +162,7 @@ aInstanceDef (CInstanceDef clName args givens methods instNameAndParams) = do
162162
case optParams of
163163
Just params -> do
164164
params' <- aExplicitParams params
165-
return $ UInstance clName' (givens' >>> params') args' methods' instName' ExplicitApp
165+
return $ UInstance clName' (catUOptAnnExplBinders givens' params') args' methods' instName' ExplicitApp
166166
Nothing -> return $ UInstance clName' givens' args' methods' instName' ImplicitApp
167167

168168
aDef :: CDef -> SyntaxM (SourceName, ULamExpr VoidS)
@@ -173,19 +173,27 @@ aDef (CDef name params optRhs optGivens body) = do
173173
effs <- fromMaybeM optEffs UPure aEffects
174174
resultTy' <- expr resultTy
175175
return (expl, Just effs, Just resultTy')
176-
implicitParams <- toNest <$> fromMaybeM optGivens [] aGivens
177-
let allParams = implicitParams >>> explicitParams
176+
implicitParams <- aOptGivens optGivens
177+
let allParams = catUOptAnnExplBinders implicitParams explicitParams
178178
body' <- block body
179179
return (name, ULamExpr allParams expl effs resultTy body')
180180

181+
catUOptAnnExplBinders :: UOptAnnExplBinders n l -> UOptAnnExplBinders l l' -> UOptAnnExplBinders n l'
182+
catUOptAnnExplBinders (expls, bs) (expls', bs') = (expls <> expls', bs >>> bs')
183+
181184
stripParens :: Group -> Group
182185
stripParens (WithSrc _ (CParens [g])) = stripParens g
183186
stripParens g = g
184187

185-
aExplicitParams :: ExplicitParams -> SyntaxM (Nest (WithExpl UOptAnnBinder) VoidS VoidS)
188+
aExplicitParams :: ExplicitParams -> SyntaxM ([Explicitness], Nest UOptAnnBinder VoidS VoidS)
186189
aExplicitParams gs = generalBinders DataParam Explicit gs
187190

188-
aGivens :: GivenClause -> SyntaxM [WithExpl UOptAnnBinder VoidS VoidS]
191+
aOptGivens :: Maybe GivenClause -> SyntaxM (UOptAnnExplBinders VoidS VoidS)
192+
aOptGivens optGivens = do
193+
(expls, implicitParams) <- unzip <$> fromMaybeM optGivens [] aGivens
194+
return (expls, toNest implicitParams)
195+
196+
aGivens :: GivenClause -> SyntaxM [(Explicitness, UOptAnnBinder VoidS VoidS)]
189197
aGivens (implicits, optConstraints) = do
190198
implicits' <- mapM (generalBinder DataParam (Inferred Nothing Unify)) implicits
191199
constraints <- fromMaybeM optConstraints [] \gs -> do
@@ -194,23 +202,24 @@ aGivens (implicits, optConstraints) = do
194202

195203
generalBinders
196204
:: ParamStyle -> Explicitness -> [Group]
197-
-> SyntaxM (Nest (WithExpl UOptAnnBinder) VoidS VoidS)
198-
generalBinders paramStyle expl params = toNest . concat <$>
199-
forM params \case
205+
-> SyntaxM ([Explicitness], Nest UOptAnnBinder VoidS VoidS)
206+
generalBinders paramStyle expl params = do
207+
(expls, bs) <- unzip . concat <$> forM params \case
200208
WithSrc _ (CGivens gs) -> aGivens gs
201209
p -> (:[]) <$> generalBinder paramStyle expl p
210+
return (expls, toNest bs)
202211

203212
generalBinder :: ParamStyle -> Explicitness -> Group
204-
-> SyntaxM (WithExpl UOptAnnBinder VoidS VoidS)
213+
-> SyntaxM (Explicitness, UOptAnnBinder VoidS VoidS)
205214
generalBinder paramStyle expl g = case expl of
206-
Inferred _ (Synth _) -> WithExpl expl <$> tyOptBinder g
215+
Inferred _ (Synth _) -> (expl,) <$> tyOptBinder g
207216
Inferred _ Unify -> do
208217
b <- binderOptTy g
209218
expl' <- return case b of
210219
UAnnBinder (UBindSource _ s) _ _ -> Inferred (Just s) Unify
211220
_ -> expl
212-
return $ WithExpl expl' b
213-
Explicit -> WithExpl expl <$> case paramStyle of
221+
return (expl', b)
222+
Explicit -> (expl,) <$> case paramStyle of
214223
TypeParam -> tyOptBinder g
215224
DataParam -> binderOptTy g
216225

@@ -338,7 +347,6 @@ effect (Binary JuxtaposeWithSpace (Identifier "State") (Identifier h)) =
338347
return $ URWSEffect State $ fromString h
339348
effect (Identifier "Except") = return UExceptionEffect
340349
effect (Identifier "IO") = return UIOEffect
341-
effect (Identifier effName) = return $ UUserEffect (fromString effName)
342350
effect _ = throw SyntaxErr "Unexpected effect form; expected one of `Read h`, `Accum h`, `State h`, `Except`, `IO`, or the name of a user-defined effect."
343351

344352
aMethod :: CSDecl -> SyntaxM (Maybe (UMethodDef VoidS))
@@ -348,7 +356,7 @@ aMethod (WithSrc src d) = Just . WithSrcE src <$> addSrcContext src case d of
348356
(name, lam) <- aDef def
349357
return $ UMethodDef (fromString name) lam
350358
CLet (WithSrc _ (CIdentifier name)) rhs -> do
351-
rhs' <- ULamExpr Empty ImplicitApp Nothing Nothing <$> block rhs
359+
rhs' <- ULamExpr ([], Empty) ImplicitApp Nothing Nothing <$> block rhs
352360
return $ UMethodDef (fromString name) rhs'
353361
_ -> throw SyntaxErr "Unexpected method definition. Expected `def` or `x = ...`."
354362

@@ -369,10 +377,10 @@ blockDecls [WithSrc src d] = addSrcContext src case d of
369377
CExpr g -> (Empty,) <$> expr g
370378
_ -> throw SyntaxErr "Block must end in expression"
371379
blockDecls (WithSrc pos (CBind b rhs):ds) = do
372-
WithExpl _ b' <- generalBinder DataParam Explicit b
380+
(_, b') <- generalBinder DataParam Explicit b
373381
rhs' <- asExpr <$> block rhs
374382
body <- block $ IndentedBlock ds
375-
let lam = ULam $ ULamExpr (UnaryNest (WithExpl Explicit b')) ExplicitApp Nothing Nothing body
383+
let lam = ULam $ ULamExpr ([Explicit], UnaryNest b') ExplicitApp Nothing Nothing body
376384
return (Empty, WithSrcE pos $ extendAppRight rhs' (ns lam))
377385
blockDecls (d:ds) = do
378386
d' <- decl PlainLet d

src/lib/Algebra.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ type BlockTraverserM i o a = SubstReaderT PolySubstVal (MaybeT1 (BuilderM SimpIR
137137
blockAsPoly
138138
:: (EnvExtender m, EnvReader m)
139139
=> Block SimpIR n -> m n (Maybe (Polynomial n))
140-
blockAsPoly (Block _ decls result) =
140+
blockAsPoly (Abs decls result) =
141141
liftBuilder $ runMaybeT1 $ runSubstReaderT idSubst $ blockAsPolyRec decls result
142142

143143
blockAsPolyRec :: Nest (Decl SimpIR) i i' -> Atom SimpIR i' -> BlockTraverserM i o (Polynomial o)

0 commit comments

Comments
 (0)