Skip to content

Commit 8b902ab

Browse files
omelkonianUlfNorell
authored andcommitted
Proper detection of local declaration
1 parent 70e325b commit 8b902ab

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

Main.hs

+9-11
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,14 @@ underAbstr_ = underAbstr __DUMMY_DOM__
151151
applyNoBodies :: Definition -> [Arg Term] -> Definition
152152
applyNoBodies d args = revert $ d `apply` args
153153
where
154-
bodies0 :: [(QName, [(Int, Maybe Term)])]
155-
bodies0 = everything (++) ([] `mkQ` go) d
156-
where go (Defn {defName = q, theDef = Function {funClauses = cls}}) =
157-
[(q, map (fmap clauseBody) (zip [0..] cls))]
154+
bodies :: [Maybe Term]
155+
bodies = map clauseBody $ funClauses $ theDef d
156+
157+
setBody cl b = cl { clauseBody = b }
158158

159159
revert :: Definition -> Definition
160-
revert d@(Defn {defName = q, theDef = f@(Function {funClauses = cls})})
161-
= d {theDef = f {funClauses = map (\(i, c) -> c {clauseBody = get i}) (zip [0..] cls)}}
162-
where get i = fromMaybe __IMPOSSIBLE__ $ join $ lookup i <$> lookup q bodies0
160+
revert d@(Defn {theDef = f@(Function {funClauses = cls})}) =
161+
d {theDef = f {funClauses = zipWith setBody cls bodies}}
163162

164163
-- Builtins ---------------------------------------------------------------
165164

@@ -297,8 +296,6 @@ compilePostulate def = do
297296
let body = hsError $ "postulate: " ++ pp ty
298297
return [ Hs.TypeSig () [x] ty
299298
, Hs.FunBind () [Hs.Match () x [] (Hs.UnGuardedRhs () body) Nothing] ]
300-
where
301-
Axiom = theDef def
302299

303300
type LocalDecls = [(QName, Definition)]
304301

@@ -338,7 +335,7 @@ compileClause locals qn x c@Clause{clauseTel = tel, namedClausePats = ps', claus
338335
((show m0 ++ "._") `isPrefixOf` show m) && (q `notElem` localUses)
339336
splitDecls :: LocalDecls -> ([(Definition, LocalDecls)], LocalDecls)
340337
splitDecls ds@((q,child):rest)
341-
| q `elem` localUses
338+
| any ((`elem` localUses) . fst) ds
342339
, (grandchildren, outer) <- span ((`belongs` q) . fst) rest
343340
, (groups, rest') <- splitDecls outer
344341
= ((child, grandchildren) : groups, rest')
@@ -353,7 +350,8 @@ compileClause locals qn x c@Clause{clauseTel = tel, namedClausePats = ps', claus
353350
children' = everywhere (mkT (`applyNoBodies` args)) children
354351

355352
-- 2. shrink calls to inner modules (unqualify + partially apply module parameters)
356-
shrinkLocalDefs t | Def q es <- t, q `elem` concatMap (\(d,ds) -> defName d : map fst ds) children
353+
localNames = concatMap (\(d,ds) -> defName d : map fst ds) children
354+
shrinkLocalDefs t | Def q es <- t, q `elem` localNames
357355
= Def (qualify_ $ qnameName q) (drop argLen es)
358356
| otherwise = t
359357
(body', children'') = everywhere (mkT shrinkLocalDefs) (body, children')

test/Where.agda

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ ex7' n₀ = go₁ n₀
109109
go₃ : Nat Nat
110110
go₃ n₃ = n₀ + n₁ + n₂ + n₃
111111

112+
ex8 : Nat
113+
ex8 = n₂
114+
where
115+
n₁ = 1
116+
n₂ = n₁ + 1
117+
112118
{-# COMPILE AGDA2HS bool2nat #-}
113119
{-# COMPILE AGDA2HS ex1 #-}
114120
{-# COMPILE AGDA2HS ex2 #-}
@@ -119,3 +125,4 @@ ex7' n₀ = go₁ n₀
119125
{-# COMPILE AGDA2HS ex6 #-}
120126
{-# COMPILE AGDA2HS ex7 #-}
121127
{-# COMPILE AGDA2HS ex7' #-}
128+
{-# COMPILE AGDA2HS ex8 #-}

test/golden/Where.hs

+7
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,10 @@ ex7' n₀ = go₁ n₀
8383
where go₃ :: Integer -> Integer
8484
go₃ n₃ = n₀ + n₁ + n₂ + n₃
8585

86+
ex8 :: Integer
87+
ex8 = n₂
88+
where n₁ :: Integer
89+
n₁ = 1
90+
n₂ :: Integer
91+
n₂ = n₁ + 1
92+

0 commit comments

Comments
 (0)