Skip to content

Commit ef10a1a

Browse files
committed
[ re #431 ] Use same strategy for inlining types as for terms
1 parent b2f8753 commit ef10a1a

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

src/Agda2Hs/Compile/Name.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ compileName n = hsName . show <$> pretty (nameConcrete n)
9191

9292
compileQName :: QName -> C (Hs.QName ())
9393
compileQName f = do
94+
whenM (isInlinedFunction f) $
95+
agda2hsErrorM $ "Failed to inline definition of" <+> prettyTCM f
9496
whenJustM (getCompileToName f) $ \g ->
9597
reportSDoc "agda2hs.compile.to" 20 $ text $
9698
"compiling name " <> prettyShow f <> " to " <> prettyShow g

src/Agda2Hs/Compile/Term.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ compileTerm ty v = do
467467
reduceProjectionLike v >>= \case
468468

469469
v@(Def f es) -> do
470-
whenM (isInlinedFunction f) $ bad "inlined function" v
471470
ty <- defType <$> getConstInfo f
472471
compileSpined (compileDef f ty) (Def f) ty es
473472

src/Agda2Hs/Compile/Type.hs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ compileType t = do
8787
reportSDoc "agda2hs.compile.type" 12 $ text "Compiling type" <+> prettyTCM t
8888
reportSDoc "agda2hs.compile.type" 22 $ text "Compiling type" <+> pretty t
8989

90+
toInline <- getInlineSymbols
91+
t <- locallyReduceDefs (OnlyReduceDefs toInline) $ reduce t
92+
9093
whenM (isErasedBaseType t) fail
9194

92-
instantiate t >>= \case
95+
case t of
9396
Pi a b -> do
9497
reportSDoc "agda2hs.compile.type" 13 $ text "Compiling pi type (" <+> prettyTCM (absName b)
9598
<+> text ":" <+> prettyTCM a <+> text ") -> " <+> underAbstraction a b prettyTCM
@@ -110,10 +113,9 @@ compileType t = do
110113
| Just args <- allApplyElims es ->
111114
ifJustM (isUnboxRecord f) (\_ -> compileUnboxType f args) $
112115
ifJustM (isTupleRecord f) (\b -> compileTupleType f b args) $
113-
ifM (isTransparentFunction f) (compileTransparentType (defType def) args) $
114-
ifM (isInlinedFunction f) (compileInlineType f es) $ do
115-
vs <- compileTypeArgs (defType def) args
116+
ifM (isTransparentFunction f) (compileTransparentType (defType def) args) $ do
116117
f <- compileQName f
118+
vs <- compileTypeArgs (defType def) args
117119
return $ tApp (Hs.TyCon () f) vs
118120
| otherwise -> fail
119121

@@ -151,10 +153,11 @@ compileType t = do
151153
compileTypeArgs :: Type -> Args -> C [Hs.Type ()]
152154
compileTypeArgs ty [] = pure []
153155
compileTypeArgs ty (x:xs) = do
156+
reportSDoc "agda2hs.compile.type" 16 $ text "compileTypeArgs ty =" <+> prettyTCM x
154157
(a, b) <- mustBePi ty
155-
reportSDoc "agda2hs.compile.type" 16 $ text "compileTypeArgs x =" <+> prettyTCM x
156-
reportSDoc "agda2hs.compile.type" 16 $ text " a =" <+> prettyTCM a
157-
reportSDoc "agda2hs.compile.type" 16 $ text " modality =" <+> prettyTCM (getModality a)
158+
reportSDoc "agda2hs.compile.type" 16 $ text " x =" <+> prettyTCM x
159+
reportSDoc "agda2hs.compile.type" 16 $ text " a =" <+> prettyTCM a
160+
reportSDoc "agda2hs.compile.type" 16 $ text " modality =" <+> prettyTCM (getModality a)
158161
let rest = compileTypeArgs (absApp b $ unArg x) xs
159162
let fail msg = agda2hsErrorM $ (text msg <> text ":") <+> parens (prettyTCM (absName b) <+> text ":" <+> prettyTCM (unDom a))
160163
compileDom a >>= \case
@@ -204,23 +207,6 @@ compileTransparentType ty args = compileTypeArgs ty args >>= \case
204207
[] -> __IMPOSSIBLE__
205208

206209

207-
compileInlineType :: QName -> Elims -> C (Hs.Type ())
208-
compileInlineType f args = do
209-
Function { funClauses = cs } <- theDef <$> getConstInfo f
210-
211-
let [ Clause { namedClausePats = pats } ] = filter (isJust . clauseBody) cs
212-
213-
when (length args < length pats) $ agda2hsErrorM $
214-
text "Cannot compile inlinable type alias" <+> prettyTCM f <+> text "as it must be fully applied."
215-
216-
r <- liftReduce $ locallyReduceDefs (OnlyReduceDefs $ Set.singleton f)
217-
$ unfoldDefinitionStep (Def f args) f args
218-
219-
case r of
220-
YesReduction _ t -> compileType t
221-
_ -> agda2hsErrorM $ text "Could not reduce inline type alias " <+> prettyTCM f
222-
223-
224210
data DomOutput = DOInstance | DODropped | DOType | DOTerm
225211

226212
compileDom :: Dom Type -> C DomOutput

test/golden/Inline.err

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
test/Fail/Inline.agda:10.1-5: error: [CustomBackendError]
2-
agda2hs:
3-
cannot compile inlined function:
4-
tail'
2+
agda2hs: Failed to inline definition of tail'

test/golden/Inline2.err

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
test/Fail/Inline2.agda:9.1-5: error: [CustomBackendError]
2-
agda2hs:
3-
cannot compile inlined function:
4-
tail'
2+
agda2hs: Failed to inline definition of tail'

0 commit comments

Comments
 (0)