Skip to content

Commit

Permalink
Closes #309: Ensures we compile pattern variables correctly by relying
Browse files Browse the repository at this point in the history
on the existing compileTypeArgs.
  • Loading branch information
VictorCMiraldo authored and jespercockx committed Jun 10, 2024
1 parent 4c750c6 commit fdbebb6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Agda2Hs/Compile/TypeDefinition.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ import Agda.Compiler.Backend

import Agda.Syntax.Common ( namedArg )
import Agda.Syntax.Internal
import Agda.Syntax.Internal.Pattern

import Agda.TypeChecking.Telescope ( mustBePi )

import Agda.Utils.Impossible ( __IMPOSSIBLE__ )
import Agda.Utils.Monad

import Agda2Hs.Compile.Type ( compileType, compileDom, DomOutput(..) )
import Agda2Hs.Compile.Type ( compileType, compileDom, DomOutput(..), compileTypeArgs )
import Agda2Hs.Compile.Types
import Agda2Hs.Compile.Utils
import Agda2Hs.Compile.Var ( compileDBVar )
import Agda2Hs.HsUtils
import Agda.Syntax.Common.Pretty (prettyShow)
import Agda.TypeChecking.Substitute


compileTypeDef :: Hs.Name () -> Definition -> C [Hs.Decl ()]
compileTypeDef name (Defn {..}) = do
unlessM (isTransparentFunction defName) $ checkValidTypeName name
Clause{..} <- singleClause funClauses
addContext (KeepNames clauseTel) $ do
as <- compileTypeArgs defType namedClausePats
as <- compileTypePatternArgs defType namedClausePats
let hd = foldl (Hs.DHApp ()) (Hs.DHead () name) as
rhs <- compileType $ fromMaybe __IMPOSSIBLE__ clauseBody
return [Hs.TypeDecl () hd rhs]
Expand All @@ -38,17 +41,15 @@ compileTypeDef name (Defn {..}) = do
[cl] -> return cl
_ -> genericError "Not supported: type definition with several clauses"


compileTypeArgs :: Type -> NAPs -> C [Hs.TyVarBind ()]
compileTypeArgs ty [] = return []
compileTypeArgs ty (p:ps) = do
(a,b) <- mustBePi ty
let rest = underAbstraction a b $ \ty' -> compileTypeArgs ty' ps
compileDom a >>= \case
DODropped -> rest
DOType -> (:) <$> compileTypeArg (namedArg p) <*> rest
DOTerm -> genericError "Not supported: type definition with term arguments"
DOInstance -> genericError "Not supported: type definition with instance arguments"
compileTypePatternArgs :: Type -> NAPs -> C [Hs.TyVarBind ()]
compileTypePatternArgs ty naps = do
aux <- compileTypeArgs ty $ fromMaybe __IMPOSSIBLE__ $ allApplyElims $ patternsToElims naps
mapM assertIsTyVarBind aux
where
assertIsTyVarBind :: Hs.Type () -> C (Hs.TyVarBind ())
assertIsTyVarBind = \case
Hs.TyVar _ n -> pure $ Hs.UnkindedVar () n
_ -> genericError "Not supported: type definition by pattern matching"

compileTypeArg :: DeBruijnPattern -> C (Hs.TyVarBind ())
compileTypeArg p@(VarP o i) = do
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import Issue264
import Issue301
import Issue305
import Issue302
import Issue309

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -163,4 +164,5 @@ import Issue264
import Issue301
import Issue305
import Issue302
import Issue309
#-}
7 changes: 7 additions & 0 deletions test/Issue309.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Issue309 where

private variable @0 a : Set

Ap : (p : @0 a Set) @0 a Set
Ap p x = p x
{-# COMPILE AGDA2HS Ap #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ import Issue264
import Issue301
import Issue305
import Issue302
import Issue309

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

type Ap p = p

0 comments on commit fdbebb6

Please sign in to comment.