Skip to content

Commit

Permalink
Closes agda#309: Ensures we compile pattern variables correctly by re…
Browse files Browse the repository at this point in the history
…lying

on the existing compileTypeArgs.
  • Loading branch information
VictorCMiraldo committed Jun 8, 2024
1 parent 9388b77 commit feb3b23
Show file tree
Hide file tree
Showing 3 changed files with 25 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
7 changes: 7 additions & 0 deletions test/Issue309.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Test where

private variable @0 a : Set

Ap : (p : @0 a Set) @0 a Set
Ap p x = p x
{-# COMPILE AGDA2HS Ap #-}
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 Test where

type Ap p = p

0 comments on commit feb3b23

Please sign in to comment.