@@ -2,6 +2,7 @@ module Agda2Hs.Compile.Term where
22
33import Control.Arrow ( (>>>) , (&&&) , second )
44import Control.Monad ( unless , zipWithM )
5+ import Control.Monad.Except
56import Control.Monad.Reader
67
78import Data.Foldable ( toList )
@@ -19,11 +20,16 @@ import Agda.Syntax.Common
1920import Agda.Syntax.Literal
2021import Agda.Syntax.Internal
2122
23+ import Agda.TypeChecking.CheckInternal ( infer )
24+ import Agda.TypeChecking.Constraints ( noConstraints )
25+ import Agda.TypeChecking.Conversion ( equalTerm )
26+ import Agda.TypeChecking.InstanceArguments ( findInstance )
27+ import Agda.TypeChecking.MetaVars ( newInstanceMeta )
2228import Agda.TypeChecking.Monad
2329import Agda.TypeChecking.Pretty
2430import Agda.TypeChecking.Records ( shouldBeProjectible , isRecordType , recordFieldNames )
2531import Agda.TypeChecking.Datatypes ( getConType )
26- import Agda.TypeChecking.Reduce ( unfoldDefinitionStep , instantiate )
32+ import Agda.TypeChecking.Reduce ( unfoldDefinitionStep , instantiate , reduce )
2733import Agda.TypeChecking.Substitute
2834import Agda.TypeChecking.Telescope ( telView , mustBePi , piApplyM , flattenTel )
2935import Agda.TypeChecking.ProjectionLike ( reduceProjectionLike )
@@ -35,7 +41,7 @@ import Agda.Utils.Monad
3541import Agda.Utils.Size
3642
3743import Agda2Hs.AgdaUtils
38- import Agda2Hs.Compile.Name ( compileQName )
44+ import Agda2Hs.Compile.Name ( compileQName , importInstance )
3945
4046import Agda2Hs.Compile.Type ( compileType , compileDom , DomOutput (.. ), compileTelSize )
4147import Agda2Hs.Compile.Types
@@ -615,3 +621,42 @@ compileLiteral (LitChar c) = return $ Hs.charE c
615621compileLiteral (LitString t) = return $ Hs. Lit () $ Hs. String () s s
616622 where s = Text. unpack t
617623compileLiteral l = genericDocError =<< text " bad term:" <?> prettyTCM (Lit l)
624+
625+
626+ checkInstance :: Term -> C ()
627+ checkInstance u = do
628+ reportSDoc " agda2hs.checkInstance" 12 $ text " checkInstance" <+> prettyTCM u
629+ reduce u >>= \ case
630+ Var x es -> do
631+ unlessM (isInstance <$> domOfBV x) illegalInstance
632+ checkInstanceElims es
633+ Def f es -> do
634+ unlessM (isJust . defInstance <$> getConstInfo f) illegalInstance
635+ importInstance f
636+ checkInstanceElims es
637+ -- We need to compile applications of `fromNat`, `fromNeg`, and
638+ -- `fromString` where the constraint type is ⊤ or IsTrue .... Ideally
639+ -- this constraint would be marked as erased but this would involve
640+ -- changing Agda builtins.
641+ Con c _ _
642+ | prettyShow (conName c) == " Agda.Builtin.Unit.tt" ||
643+ prettyShow (conName c) == " Haskell.Prim.IsTrue.itsTrue" ||
644+ prettyShow (conName c) == " Haskell.Prim.IsFalse.itsFalse" -> return ()
645+ _ -> illegalInstance
646+
647+ where
648+ illegalInstance :: C ()
649+ illegalInstance = do
650+ reportSDoc " agda2hs.checkInstance" 15 $ text " illegal instance: " <+> pretty u
651+ genericDocError =<< text " illegal instance: " <+> prettyTCM u
652+
653+ checkInstanceElims :: Elims -> C ()
654+ checkInstanceElims = mapM_ checkInstanceElim
655+
656+ checkInstanceElim :: Elim -> C ()
657+ checkInstanceElim (Apply v) =
658+ when (isInstance v && usableQuantity v) $
659+ checkInstance $ unArg v
660+ checkInstanceElim IApply {} = illegalInstance
661+ checkInstanceElim (Proj _ f) =
662+ unlessM (isInstance . defArgInfo <$> getConstInfo f) illegalInstance
0 commit comments