Skip to content

Commit 4d591a1

Browse files
committed
Use names for dot patterns
Still a bit imperfect: names corresponding to module parameters are not prefixed by '_'s because these are generated purely based of the type, while explicitly named dot patterns can have '_'s prefixed
1 parent 41a38a0 commit 4d591a1

File tree

8 files changed

+60
-15
lines changed

8 files changed

+60
-15
lines changed

src/Agda2Hs/Compile/Function.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Agda.Utils.Size ( Sized(size) )
3737

3838
import Agda2Hs.AgdaUtils
3939
import Agda2Hs.Compile.Name ( compileQName )
40-
import Agda2Hs.Compile.Term ( compileTerm, usableDom, dependentDom )
40+
import Agda2Hs.Compile.Term ( compileTerm, usableDom, dependentDom, extractMaybeName )
4141
import Agda2Hs.Compile.Type ( compileType, compileDom, DomOutput(..), compileDomType )
4242
import Agda2Hs.Compile.TypeDefinition ( compileTypeDef )
4343
import Agda2Hs.Compile.Types
@@ -294,7 +294,8 @@ compilePats ty ((namedArg -> ProjP po pn):ps) = do
294294

295295
compilePats (absBody b) ps
296296

297-
compilePats ty ((namedArg -> pat):ps) = do
297+
compilePats ty (p:ps) = do
298+
let pat = namedArg p
298299
(a, b) <- mustBePi ty
299300
reportSDoc "agda2hs.compile.pattern" 10 $ text "Compiling pattern:" <+> prettyTCM pat
300301
let rest = compilePats (absApp b (patternToTerm pat)) ps
@@ -304,21 +305,21 @@ compilePats ty ((namedArg -> pat):ps) = do
304305
DOType -> rest
305306
DOTerm -> do
306307
checkNoAsPatterns pat
307-
(:) <$> compilePat (unDom a) pat <*> rest
308+
(:) <$> compilePat (unDom a) (extractMaybeName p) pat <*> rest
308309

309310

310-
compilePat :: Type -> DeBruijnPattern -> C (Hs.Pat ())
311+
compilePat :: Type -> Maybe ArgName -> DeBruijnPattern -> C (Hs.Pat ())
311312

312313
-- variable pattern
313-
compilePat ty p@(VarP o x)
314+
compilePat ty _ p@(VarP o x)
314315
| PatOWild <- patOrigin o = return $ Hs.PWildCard ()
315316
| otherwise = do
316317
n <- hsName <$> compileDBVar (dbPatVarIndex x)
317318
checkValidVarName n
318319
return $ Hs.PVar () n
319320

320321
-- special constructor pattern
321-
compilePat ty (ConP ch i ps) = do
322+
compilePat ty _ (ConP ch i ps) = do
322323
Just ((_, _, _), ty) <- getConType ch =<< reduce ty
323324
let c = conName ch
324325

@@ -330,13 +331,18 @@ compilePat ty (ConP ch i ps) = do
330331
return $ pApp c ps
331332

332333
-- literal patterns
333-
compilePat ty (LitP _ l) = compileLitPat l
334+
compilePat ty _ (LitP _ l) = compileLitPat l
334335

335-
-- dot patterns are compiled to wildcard patterns
336-
compilePat _ (DotP _ _) = return $ Hs.PWildCard ()
336+
-- dot patterns are compiled by prefixing with an '_' if it has been given
337+
-- an explicit name, or a wildcard otherwise
338+
compilePat _ (Just n) (DotP _ _) = do
339+
let n' = hsName $ '_' : n
340+
checkValidVarName n'
341+
return $ Hs.PVar () n'
342+
compilePat _ Nothing (DotP _ _) = return $ Hs.PWildCard ()
337343

338344
-- nothing else is supported
339-
compilePat _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p
345+
compilePat _ _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p
340346

341347

342348
compileErasedConP :: Type -> Strictness -> NAPs -> C (Hs.Pat ())

src/Agda2Hs/Compile/Term.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,7 @@ compileInlineFunctionApp f ty args = do
576576
extractPatName _ = __IMPOSSIBLE__
577577

578578
extractName :: NamedArg DeBruijnPattern -> ArgName
579-
extractName (unArg -> np)
580-
| Just n <- nameOf np = rangedThing (woThing n)
581-
| otherwise = extractPatName (namedThing np)
579+
extractName p = fromMaybe (extractPatName $ namedArg p) (extractMaybeName p)
582580

583581
etaExpand :: NAPs -> Type -> [Term] -> C Term
584582
etaExpand [] ty args = do
@@ -594,6 +592,8 @@ compileInlineFunctionApp f ty args = do
594592
let ai = domInfo dom
595593
Lam ai . mkAbs (extractName p) <$> etaExpand ps (absBody cod) (raise 1 args ++ [ var 0 ])
596594

595+
extractMaybeName :: NamedArg a -> Maybe ArgName
596+
extractMaybeName p = rangedThing . woThing <$> nameOf (unArg p)
597597

598598
compileApp :: Hs.Exp () -> Type -> [Term] -> C (Hs.Exp ())
599599
compileApp = compileApp' . eApp

test/AllTests.agda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import Issue353
8989
import RankNTypes
9090
import ErasedPatternLambda
9191
import ErasedPatternLambda2
92+
import ErasedPatternLambda3
9293
import CustomTuples
9394
import ProjectionLike
9495
import FunCon
@@ -190,6 +191,7 @@ import Issue353
190191
import RankNTypes
191192
import ErasedPatternLambda
192193
import ErasedPatternLambda2
194+
import ErasedPatternLambda3
193195
import CustomTuples
194196
import ProjectionLike
195197
import FunCon

test/ErasedPatternLambda2.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ caseTelBind (ExtendTel a tel) f = f a tel refl
1414
{-# COMPILE AGDA2HS caseTelBind #-}
1515

1616
checkSubst' : {@0 x α β} (t : Telescope α (x ∷ β)) (ty : Bool) (rest : Telescope (x ∷ α) β) @0 t ≡ ExtendTel ty rest Bool
17-
checkSubst' .(ExtendTel ty rest) ty rest refl = True
17+
checkSubst' t ty rest refl = True
1818
{-# COMPILE AGDA2HS checkSubst' #-}
1919

2020
checkSubst : {@0 x α β} (t : Telescope α (x ∷ β)) Bool

test/ErasedPatternLambda3.agda

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
open import Haskell.Prelude
2+
3+
Scope = List Bool
4+
5+
data Telescope (@0 α : Scope) : @0 Scope Type where
6+
ExtendTel : {@0 x β} Bool Telescope (x ∷ α) β Telescope α (x ∷ β)
7+
{-# COMPILE AGDA2HS Telescope #-}
8+
9+
caseTelBind : {@0 x α β} (tel : Telescope α (x ∷ β))
10+
((a : Bool) (rest : Telescope (x ∷ α) β) @0 tel ≡ ExtendTel a rest d)
11+
d
12+
caseTelBind (ExtendTel a tel) f = f a tel refl
13+
14+
{-# COMPILE AGDA2HS caseTelBind #-}
15+
16+
module _ {@0 x α β} (t : Telescope α (x ∷ β)) where
17+
checkSubst' : (ty : Bool) (rest : Telescope (x ∷ α) β) @0 t ≡ ExtendTel ty rest Bool
18+
checkSubst' ty rest refl = True
19+
{-# COMPILE AGDA2HS checkSubst' #-}
20+
21+
checkSubst : Bool
22+
checkSubst = caseTelBind t checkSubst'
23+
{-# COMPILE AGDA2HS checkSubst #-}

test/golden/AllTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import Issue353
8484
import RankNTypes
8585
import ErasedPatternLambda
8686
import ErasedPatternLambda2
87+
import ErasedPatternLambda3
8788
import CustomTuples
8889
import ProjectionLike
8990
import FunCon

test/golden/ErasedPatternLambda2.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ caseTelBind :: Telescope -> (Bool -> Telescope -> d) -> d
66
caseTelBind (ExtendTel a tel) f = f a tel
77

88
checkSubst' :: Telescope -> Bool -> Telescope -> Bool
9-
checkSubst' _ ty rest = True
9+
checkSubst' _t ty rest = True
1010

1111
checkSubst :: Telescope -> Bool
1212
checkSubst t = caseTelBind t (checkSubst' t)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ErasedPatternLambda3 where
2+
3+
data Telescope = ExtendTel Bool Telescope
4+
5+
caseTelBind :: Telescope -> (Bool -> Telescope -> d) -> d
6+
caseTelBind (ExtendTel a tel) f = f a tel
7+
8+
checkSubst' :: Telescope -> Bool -> Telescope -> Bool
9+
checkSubst' t ty rest = True
10+
11+
checkSubst :: Telescope -> Bool
12+
checkSubst t = caseTelBind t (checkSubst' t)
13+

0 commit comments

Comments
 (0)