Skip to content

Commit

Permalink
fix module application
Browse files Browse the repository at this point in the history
  • Loading branch information
flupe committed Nov 24, 2023
1 parent 1894a8d commit 643a055
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 19 deletions.
16 changes: 16 additions & 0 deletions src/Agda2Hs/AgdaUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Agda.Syntax.TopLevelModuleName
import Agda.TypeChecking.Monad ( topLevelModuleName )
import Agda.TypeChecking.Pretty
import Agda.TypeChecking.Substitute
import Agda.TypeChecking.Reduce ( reduceDefCopy )

import Agda.Utils.Either ( isRight )
import Agda.Utils.List ( initMaybe )
Expand Down Expand Up @@ -101,3 +102,18 @@ getTopLevelModuleForQName = getTopLevelModuleForModuleName . qnameModule
lookupModuleInCurrentModule :: C.Name -> TCM [AbstractModule]
lookupModuleInCurrentModule x =
List1.toList' . Map.lookup x . nsModules . thingsInScope [PublicNS, PrivateNS] <$> getCurrentScope

-- | Try to unfold a definition if introduced by module application.
maybeUnfoldCopy
:: PureTCM m
=> QName -- ^ Name of the definition.
-> Elims
-> (Term -> m a)
-- ^ Callback if the definition is indeed a copy.
-> (QName -> Elims -> m a)
-- ^ Callback if the definition isn't a copy.
-> m a
maybeUnfoldCopy f es onTerm onDef =
reduceDefCopy f es >>= \case
NoReduction () -> onDef f es
YesReduction _ t -> onTerm t
49 changes: 31 additions & 18 deletions src/Agda2Hs/Compile/Term.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,25 +230,29 @@ compileTerm v = do
hsVar s `app` es
-- v currently we assume all record projections are instance
-- args that need attention
Def f es
Def f es -> maybeUnfoldCopy f es compileTerm $ \f es -> if
| Just semantics <- isSpecialTerm f -> do
reportSDoc "agda2hs.compile.term" 12 $ text "Compiling application of special function"
semantics f es
reportSDoc "agda2hs.compile.term" 12 $ text "Compiling application of special function"
semantics f es
| otherwise -> isClassFunction f >>= \case
True -> compileClassFunApp f es
False -> (isJust <$> isUnboxProjection f) `or2M` isTransparentFunction f >>= \case
True -> compileErasedApp es
False -> do
reportSDoc "agda2hs.compile.term" 12 $ text "Compiling application of regular function"
-- Drop module parameters of local `where` functions
moduleArgs <- getDefFreeVars f
reportSDoc "agda2hs.compile.term" 15 $ text "Module arguments for" <+> (prettyTCM f <> text ":") <+> prettyTCM moduleArgs
(`app` drop moduleArgs es) . Hs.Var () =<< compileQName f
Con h i es
| Just semantics <- isSpecialCon (conName h) -> semantics h i es
Con h i es -> isUnboxConstructor (conName h) >>= \case
Just _ -> compileErasedApp es
Nothing -> (`app` es) . Hs.Con () =<< compileQName (conName h)
True -> compileClassFunApp f es
False -> (isJust <$> isUnboxProjection f) `or2M` isTransparentFunction f >>= \case
True -> compileErasedApp es
False -> do
reportSDoc "agda2hs.compile.term" 12 $ text "Compiling application of regular function"
-- Drop module parameters of local `where` functions
moduleArgs <- getDefFreeVars f
reportSDoc "agda2hs.compile.term" 15 $ text "Module arguments for" <+> (prettyTCM f <> text ":") <+> prettyTCM moduleArgs
(`app` drop moduleArgs es) . Hs.Var () =<< compileQName f
Con h i es -> do
reportSDoc "agda2hs.compile" 8 $ text "reached constructor:" <+> prettyTCM (conName h)
-- the constructor may be a copy introduced by module application,
-- therefore we need to find the original constructor
info <- getConstInfo (conName h)
if not (defCopy info)
then compileCon h i es
else let Constructor{conSrcCon = c} = theDef info in
compileCon c ConOSystem es
Lit l -> compileLiteral l
Lam v b | usableModality v, getOrigin v == UserWritten -> do
when (patternInTeleName `isPrefixOf` absName b) $ genericDocError =<< do
Expand All @@ -272,7 +276,16 @@ compileTerm v = do
t -> genericDocError =<< text "bad term:" <?> prettyTCM t
where
app :: Hs.Exp () -> Elims -> C (Hs.Exp ())
app hd es = eApp <$> pure hd <*> compileElims es
app hd es = eApp hd <$> compileElims es

compileCon :: ConHead -> ConInfo -> Elims -> C (Hs.Exp ())
compileCon h i es
| Just semantics <- isSpecialCon (conName h)
= semantics h i es
compileCon h i es =
isUnboxConstructor (conName h) >>= \case
Just _ -> compileErasedApp es
Nothing -> (`app` es) . Hs.Con () =<< compileQName (conName h)

-- `compileErasedApp` compiles an application of an erased constructor
-- or projection.
Expand Down
4 changes: 3 additions & 1 deletion src/Agda2Hs/Compile/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ compileType' t = do
compileType :: Term -> C (Hs.Type ())
compileType t = do
reportSDoc "agda2hs.compile.type" 12 $ text "Compiling type" <+> prettyTCM t
reportSDoc "agda2hs.compile.type" 22 $ text "Compiling type" <+> pretty t

case t of
Pi a b -> compileDom (absName b) a >>= \case
DomType _ hsA -> do
Expand All @@ -159,7 +161,7 @@ compileType t = do
hsB <- underAbstraction a b (compileType . unEl)
return $ constrainType hsA hsB
DomDropped -> underAbstr a b (compileType . unEl)
Def f es -> do
Def f es -> maybeUnfoldCopy f es compileType $ \f es -> do
def <- getConstInfo f
if | not (usableModality def) ->
genericDocError
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import Issue200
import Issue169
import Issue210
import ModuleParameters
import ModuleParametersImports

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -122,4 +123,5 @@ import Issue200
import Issue169
import Issue210
import ModuleParameters
import ModuleParametersImports
#-}
10 changes: 10 additions & 0 deletions test/ModuleParametersImports.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ModuleParametersImports where

open import Haskell.Prelude
open import ModuleParameters Bool (λ _ Nat)

scope : Scope
scope = unbind (Bind True 3 (Bind False 2 Empty))
{-# COMPILE AGDA2HS scope #-}


1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ import Issue200
import Issue169
import Issue210
import ModuleParameters
import ModuleParametersImports

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

import qualified ModuleParameters (Scope(Bind, Empty), unbind)
import Numeric.Natural (Natural)

scope :: ModuleParameters.Scope Natural
scope
= ModuleParameters.unbind
(ModuleParameters.Bind 3
(ModuleParameters.Bind 2 ModuleParameters.Empty))

0 comments on commit 643a055

Please sign in to comment.