Skip to content

Commit

Permalink
[ re #357 ] Rewrite \x -> (a -> x) to (->) a and \x -> ((->) x)
Browse files Browse the repository at this point in the history
… to `(->)`
  • Loading branch information
jespercockx committed Sep 19, 2024
1 parent c28556d commit 6715421
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Agda2Hs/Compile/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,21 @@ compileType t = do

Sort s -> return (Hs.TyStar ())

-- TODO: we should also drop lambdas that can be erased based on their type
-- (e.g. argument is of type Level/Size or in a Prop) but currently we do
-- not have access to the type of the lambda here.
Lam argInfo restAbs
| hasQuantity0 argInfo -> underAbstraction_ restAbs compileType
| otherwise -> genericDocError =<< text "Not supported: type-level lambda" <+> prettyTCM t
Lam argInfo restAbs -> do
(body , x0) <- underAbstraction_ restAbs $ \b ->
(,) <$> compileType b <*> (hsName <$> compileDBVar 0)

-- TODO: we should also drop lambdas that can be erased based on their type
-- (e.g. argument is of type Level/Size or in a Prop) but currently we do
-- not have access to the type of the lambda here.
if | hasQuantity0 argInfo -> return body
-- Rewrite `\x -> (a -> x)` to `(->) a`
| Hs.TyFun _ a (Hs.TyVar _ y) <- body
, y == x0 -> return $ Hs.TyApp () (Hs.TyCon () $ Hs.Special () $ Hs.FunCon ()) a
-- Rewrite `\x -> ((->) x)` to `(->)`
| Hs.TyApp _ (Hs.TyCon _ (Hs.Special _ (Hs.FunCon _))) (Hs.TyVar _ y) <- body
, y == x0 -> return $ Hs.TyCon () $ Hs.Special () $ Hs.FunCon ()
| otherwise -> genericDocError =<< text "Not supported: type-level lambda" <+> prettyTCM t

_ -> fail
where fail = genericDocError =<< text "Bad Haskell type:" <?> prettyTCM t
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import Issue317
import ErasedPatternLambda
import CustomTuples
import ProjectionLike
import FunCon

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -173,4 +174,5 @@ import Issue317
import ErasedPatternLambda
import CustomTuples
import ProjectionLike
import FunCon
#-}
22 changes: 22 additions & 0 deletions test/FunCon.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

open import Haskell.Prelude

data D1 (t : Set Set) : Set where
C1 : t Bool D1 t

{-# COMPILE AGDA2HS D1 #-}

f1 : D1 (λ a Int a)
f1 = C1 (_== 0)

{-# COMPILE AGDA2HS f1 #-}

data D2 (t : Set Set Set) : Set where
C2 : t Int Int D2 t

{-# COMPILE AGDA2HS D2 #-}

f2 : D2 (λ a b a b)
f2 = C2 (_+ 1)

{-# COMPILE AGDA2HS f2 #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ import Issue317
import ErasedPatternLambda
import CustomTuples
import ProjectionLike
import FunCon

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

data D1 t = C1 (t Bool)

f1 :: D1 ((->) Int)
f1 = C1 (== 0)

data D2 t = C2 (t Int Int)

f2 :: D2 (->)
f2 = C2 (+ 1)

0 comments on commit 6715421

Please sign in to comment.