Skip to content

Commit b2f8753

Browse files
Compile dot patterns (#440)
* Compile dot patterns to wildcards Arguably it might be nice to retain names (e.g. prefixed by an |_|) when the dot pattern isn't provided explicitly, but this info does not appear to be retained by the time we reach internal syntax * 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 * Stop prefixing with '_'s This stays consistent with the behaviour on module parameters and has less chance of weird edge-cases * Rename new tests I got rid of the pattern lambdas to make them, so the test name doesn't really make sense anymore * Small refactor + new test Rather than trying to extract the dot pattern name with nameOf, we make use of the PatOrigin in PatternInfo
1 parent 401ad9c commit b2f8753

File tree

10 files changed

+129
-3
lines changed

10 files changed

+129
-3
lines changed

src/Agda2Hs/Compile/Function.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ compilePat ty (ConP ch i ps) = do
332332
-- literal patterns
333333
compilePat ty (LitP _ l) = compileLitPat l
334334

335+
-- "Inferred" dot patterns that the programmer has explicitly named are
336+
-- compiled to variable patterns using that given name, wildcards otherwise
337+
compilePat _ (DotP (PatternInfo (PatOVar n) _) _) = do
338+
let n' = hsName $ prettyShow n
339+
checkValidVarName n'
340+
return $ Hs.PVar () n'
341+
compilePat _ (DotP (PatternInfo PatODot _) _) =
342+
return $ Hs.PWildCard ()
335343

336344
-- nothing else is supported
337345
compilePat _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ import Issue408
103103
import CompileTo
104104
import RuntimeCast
105105
import Issue306
106+
import RelevantDotPattern1
107+
import RelevantDotPattern2
108+
import RelevantDotPattern3
106109

107110
{-# FOREIGN AGDA2HS
108111
import Issue14
@@ -203,4 +206,7 @@ import Issue408
203206
import CompileTo
204207
import RuntimeCast
205208
import Issue306
209+
import RelevantDotPattern1
210+
import RelevantDotPattern2
211+
import RelevantDotPattern3
206212
#-}

test/RelevantDotPattern1.agda

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

test/RelevantDotPattern2.agda

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

test/RelevantDotPattern3.agda

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

test/golden/AllTests.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,7 @@ import Issue408
9898
import CompileTo
9999
import RuntimeCast
100100
import Issue306
101+
import RelevantDotPattern1
102+
import RelevantDotPattern2
103+
import RelevantDotPattern3
101104

test/golden/RelevantDotPattern1.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RelevantDotPattern1 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+

test/golden/RelevantDotPattern2.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RelevantDotPattern2 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+

test/golden/RelevantDotPattern3.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RelevantDotPattern3 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' (ExtendTel ty' rest') ty rest = True
10+
11+
checkSubst :: Telescope -> Bool
12+
checkSubst t = caseTelBind t (checkSubst' t)
13+

0 commit comments

Comments
 (0)