Skip to content

Commit

Permalink
[ fix #340 ] Compile pattern lambda to regular lambda if all proper p…
Browse files Browse the repository at this point in the history
…atterns are erased
  • Loading branch information
jespercockx committed Jul 22, 2024
1 parent 0fc20cc commit f0bd92a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Agda2Hs/Compile/Term.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,20 @@ lambdaCase q ty args = compileLocal $ setCurrentRangeQ q $ do
cs <- mapMaybeM (compileClause' mname (Just q) (hsName "(lambdaCase)") ty') cs

case cs of
-- If there is a single clause and all patterns got erased, we
-- simply return the body. do
[Hs.Match _ _ [] (Hs.UnGuardedRhs _ rhs) _] -> return rhs
-- If there is a single clause and all proper patterns got erased,
-- we turn the remaining arguments into normal lambdas.
[Hs.Match _ _ ps (Hs.UnGuardedRhs _ rhs) _]
| null ps -> return rhs
| all isVarPat ps -> return $ Hs.Lambda () ps rhs
_ -> do
lcase <- hsLCase =<< mapM clauseToAlt cs -- Pattern lambdas cannot have where blocks
eApp lcase <$> compileArgs ty' rest
-- undefined -- compileApp lcase (undefined, undefined, rest)

where
isVarPat :: Hs.Pat () -> Bool
isVarPat Hs.PVar{} = True
isVarPat _ = False

-- | Compile @if_then_else_@ to a Haskell @if ... then ... else ... @ expression.
ifThenElse :: DefCompileRule
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import Issue305
import Issue302
import Issue309
import Issue317
import ErasedPatternLambda

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -167,4 +168,5 @@ import Issue305
import Issue302
import Issue309
import Issue317
import ErasedPatternLambda
#-}
18 changes: 18 additions & 0 deletions test/ErasedPatternLambda.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open import Haskell.Prelude

Scope = List Bool

data Telescope (@0 α : Scope) : @0 Scope Set where
ExtendTel : {@0 x β} Bool Telescope (x ∷ α) β Telescope α (x ∷ β)
{-# COMPILE AGDA2HS Telescope #-}

caseTelBind : {@0 x α β} (tel : Telescope α (x ∷ β))
((a : Bool) (rest : Telescope (x ∷ α) β) @0 tel ≡ ExtendTel a rest d)
d
caseTelBind (ExtendTel a tel) f = f a tel refl

{-# COMPILE AGDA2HS caseTelBind #-}

checkSubst : {@0 x α β} (t : Telescope α (x ∷ β)) Bool
checkSubst t = caseTelBind t λ ty rest λ where refl True
{-# COMPILE AGDA2HS checkSubst #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ import Issue305
import Issue302
import Issue309
import Issue317
import ErasedPatternLambda

10 changes: 10 additions & 0 deletions test/golden/ErasedPatternLambda.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ErasedPatternLambda where

data Telescope = ExtendTel Bool Telescope

caseTelBind :: Telescope -> (Bool -> Telescope -> d) -> d
caseTelBind (ExtendTel a tel) f = f a tel

checkSubst :: Telescope -> Bool
checkSubst t = caseTelBind t (\ ty rest -> True)

0 comments on commit f0bd92a

Please sign in to comment.