Skip to content

Commit e58fe32

Browse files
committed
Rec pat edge case + more tests
1 parent 293b30c commit e58fe32

File tree

9 files changed

+107
-10
lines changed

9 files changed

+107
-10
lines changed

src/Agda2Hs/Compile/Function.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Agda2Hs.Compile.Var ( compileDBVar )
4646

4747
import qualified Agda2Hs.Language.Haskell as Hs
4848
import Agda2Hs.Language.Haskell.Utils
49-
( Strictness, hsName, pApp, patToExp, constrainType, qualifyType )
49+
( Strictness, hsName, pApp, patToExp, constrainType, qualifyType, hsUnqualName )
5050

5151
-- | Compilation rules for specific constructors in patterns.
5252
isSpecialCon :: QName -> Maybe (Type -> NAPs -> C (Hs.Pat ()))
@@ -305,21 +305,21 @@ compilePats ty (p:ps) = do
305305
DOType -> rest
306306
DOTerm -> do
307307
checkNoAsPatterns pat
308-
(:) <$> compilePat (unDom a) (extractMaybeName p) pat <*> rest
308+
(:) <$> compilePat (unDom a) pat <*> rest
309309

310310

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

313313
-- variable pattern
314-
compilePat ty _ p@(VarP o x)
314+
compilePat ty p@(VarP o x)
315315
| PatOWild <- patOrigin o = return $ Hs.PWildCard ()
316316
| otherwise = do
317317
n <- hsName <$> compileDBVar (dbPatVarIndex x)
318318
checkValidVarName n
319319
return $ Hs.PVar () n
320320

321321
-- special constructor pattern
322-
compilePat ty _ (ConP ch i ps) = do
322+
compilePat ty (ConP ch i ps) = do
323323
Just ((_, _, _), ty) <- getConType ch =<< reduce ty
324324
let c = conName ch
325325

@@ -331,18 +331,23 @@ compilePat ty _ (ConP ch i ps) = do
331331
return $ pApp c ps
332332

333333
-- literal patterns
334-
compilePat ty _ (LitP _ l) = compileLitPat l
334+
compilePat ty (LitP _ l) = compileLitPat l
335335

336336
-- "Inferred" dot patterns that the programmer has explicitly named are
337337
-- compiled to variable patterns using that given name, wildcards otherwise
338-
compilePat _ (Just n) (DotP _ _) = do
339-
let n' = hsName n
338+
compilePat _ (DotP (PatternInfo (PatOVar n) _) _) = do
339+
let n' = hsName $ prettyShow n
340340
checkValidVarName n'
341341
return $ Hs.PVar () n'
342-
compilePat _ Nothing (DotP _ _) = return $ Hs.PWildCard ()
342+
compilePat ty (DotP (PatternInfo PatORec (c:ps)) _) =
343+
-- User-written record pattern which has been elaborated to a dot pattern
344+
return $ pApp (hsUnqualName $ prettyShow c)
345+
(Hs.PVar () . hsName . prettyShow <$> ps)
346+
compilePat _ (DotP (PatternInfo PatODot _) _) =
347+
return $ Hs.PWildCard ()
343348

344349
-- nothing else is supported
345-
compilePat _ _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p
350+
compilePat _ p = agda2hsErrorM $ "bad pattern:" <?> prettyTCM p
346351

347352

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

test/AllTests.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ import RuntimeCast
105105
import Issue306
106106
import RelevantDotPattern1
107107
import RelevantDotPattern2
108+
import RelevantDotPattern3
109+
import RelevantDotPattern4
110+
import RelevantDotPattern5
108111

109112
{-# FOREIGN AGDA2HS
110113
import Issue14
@@ -207,4 +210,7 @@ import RuntimeCast
207210
import Issue306
208211
import RelevantDotPattern1
209212
import RelevantDotPattern2
213+
import RelevantDotPattern3
214+
import RelevantDotPattern4
215+
import RelevantDotPattern5
210216
#-}

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/RelevantDotPattern4.agda

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Derived from "ErasedPatternLambda"
2+
open import Haskell.Prelude
3+
4+
record Foo : Set where
5+
no-eta-equality
6+
pattern
7+
field
8+
x : Nat
9+
y : Nat
10+
open Foo public
11+
12+
{-# COMPILE AGDA2HS Foo #-}
13+
14+
foo : (xy : Foo) x y @0 xy ≡ record {x = x; y = y} Nat
15+
foo record {x = x'; y = y'} x y _ = x
16+
17+
{-# COMPILE AGDA2HS foo #-}

test/RelevantDotPattern5.agda

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Derived from "ErasedPatternLambda"
2+
open import Haskell.Prelude
3+
4+
data Foo : Set where
5+
Mk : Nat Nat Foo
6+
7+
{-# COMPILE AGDA2HS Foo #-}
8+
9+
foo : (xy : Foo) x y @0 xy ≡ Mk x y Nat
10+
foo (Mk x' y') x y _ = x
11+
12+
{-# COMPILE AGDA2HS foo #-}

test/golden/AllTests.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,7 @@ import RuntimeCast
100100
import Issue306
101101
import RelevantDotPattern1
102102
import RelevantDotPattern2
103+
import RelevantDotPattern3
104+
import RelevantDotPattern4
105+
import RelevantDotPattern5
103106

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+

test/golden/RelevantDotPattern4.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module RelevantDotPattern4 where
2+
3+
import Numeric.Natural (Natural)
4+
5+
data Foo = Foo{x :: Natural, y :: Natural}
6+
7+
foo :: Foo -> Natural -> Natural -> Natural
8+
foo (Foo x' y') x y = x
9+

test/golden/RelevantDotPattern5.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module RelevantDotPattern5 where
2+
3+
import Numeric.Natural (Natural)
4+
5+
data Foo = Mk Natural Natural
6+
7+
foo :: Foo -> Natural -> Natural -> Natural
8+
foo (Mk x' y') x y = x
9+

0 commit comments

Comments
 (0)