module Agda.TypeChecking.Rewriting where
import Prelude hiding (null)
import Control.Applicative hiding (empty)
import Control.Monad
import Control.Monad.Reader (local)
import Data.Foldable ( Foldable, foldMap )
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import qualified Data.List as List
import Data.Monoid
import Agda.Interaction.Options
import Agda.Syntax.Common
import Agda.Syntax.Internal as I
import Agda.TypeChecking.Monad
import Agda.TypeChecking.Monad.Builtin
import Agda.TypeChecking.EtaContract
import Agda.TypeChecking.Free
import Agda.TypeChecking.MetaVars
import Agda.TypeChecking.Conversion
import Agda.TypeChecking.Pretty
import Agda.TypeChecking.Reduce
import Agda.TypeChecking.Substitute
import Agda.TypeChecking.Telescope
import Agda.TypeChecking.Rewriting.NonLinMatch
import qualified Agda.TypeChecking.Reduce.Monad as Red
import Agda.Utils.Functor
import Agda.Utils.Maybe
import Agda.Utils.Monad
import Agda.Utils.Null
import Agda.Utils.Singleton
import Agda.Utils.Size
#include "undefined.h"
import Agda.Utils.Impossible
requireOptionRewriting :: TCM ()
requireOptionRewriting =
unlessM (optRewriting <$> pragmaOptions) $ typeError NeedOptionRewriting
verifyBuiltinRewrite :: Term -> Type -> TCM ()
verifyBuiltinRewrite v t = do
requireOptionRewriting
let failure reason = typeError . GenericDocError =<< sep
[ prettyTCM v <+> text " does not have the right type for a rewriting relation"
, reason
]
caseMaybeM (relView t)
(failure $ text "because it should accept at least two arguments") $
\ (RelView tel delta a b core) -> do
case ignoreSharing (unEl core) of
Sort{} -> do
unlessM (tryConversion $
inTopContext $ addContext tel $ escapeContext 1 $
equalType (raise 1 a) b) $
failure $ text $ "because the types of the last two arguments are different"
Con{} -> __IMPOSSIBLE__
Level{} -> __IMPOSSIBLE__
Lam{} -> __IMPOSSIBLE__
Pi{} -> __IMPOSSIBLE__
Shared{} -> __IMPOSSIBLE__
_ -> failure $ text "because its type does not end in a sort, but in "
<+> do inTopContext $ addContext tel $ prettyTCM core
data RelView = RelView
{ relViewTel :: Telescope
, relViewDelta :: ListTel
, relViewType :: Type
, relViewType' :: Type
, relViewCore :: Type
}
relView :: Type -> TCM (Maybe RelView)
relView t = do
TelV tel core <- telView t
let n = size tel
(delta, lastTwo) = splitAt (n 2) $ telToList tel
if size lastTwo < 2 then return Nothing else do
let [a, b] = snd . unDom <$> lastTwo
return $ Just $ RelView tel delta a b core
addRewriteRule :: QName -> TCM ()
addRewriteRule q = do
requireOptionRewriting
Def rel _ <- primRewrite
def <- instantiateDef =<< getConstInfo q
when (isEmptyFunction $ theDef def) $
typeError . GenericDocError =<< hsep
[ text "Rewrite rule from function "
, prettyTCM q
, text " cannot be added before the function definition"
]
relV <- relView =<< do defType <$> getConstInfo rel
let RelView _tel delta a _a' _core =
fromMaybe __IMPOSSIBLE__ relV
reportSDoc "rewriting" 30 $ do
text "rewrite relation at type " <+> do
inTopContext $ prettyTCM (telFromList delta) <+> text " |- " <+> do
addContext delta $ prettyTCM a
TelV gamma1 core <- telView $ defType def
reportSDoc "rewriting" 30 $ do
text "attempting to add rewrite rule of type " <+> do
prettyTCM gamma1 <+> text " |- " <+> do
addContext gamma1 $ prettyTCM core
let failureWrongTarget = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " does not target rewrite relation" ]
let failureMetas = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " is not a legal rewrite rule, since it contains unsolved meta variables" ]
let failureNotDefOrCon = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " is not a legal rewrite rule, since the left-hand side is neither a defined symbol nor a constructor" ]
let failureFreeVars xs = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " is not a legal rewrite rule, since the following variables are not bound by the left hand side: " , prettyList_ (map (prettyTCM . var) $ IntSet.toList xs) ]
let failureLhsReduction lhs = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " is not a legal rewrite rule, since the left-hand side " , prettyTCM lhs , text " has top-level reductions" ]
let failureIllegalRule = typeError . GenericDocError =<< hsep
[ prettyTCM q , text " is not a legal rewrite rule" ]
case ignoreSharing $ unEl core of
Def rel' es@(_:_:_) | rel == rel' -> do
let vs = map unArg $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es
n = size vs
(us, [lhs, rhs]) = splitAt (n 2) vs
unless (size delta == size us) __IMPOSSIBLE__
b <- instantiateFull $ applySubst (parallelS $ reverse us) a
gamma0 <- getContextTelescope
gamma1 <- instantiateFull gamma1
let gamma = gamma0 `abstract` gamma1
unless (null $ allMetas (telToList gamma1)) failureMetas
f <- case ignoreSharing lhs of
Def f es -> return f
Con c vs -> return $ conName c
_ -> failureNotDefOrCon
rew <- addContext gamma1 $ do
lhs <- normaliseArgs lhs
unlessM (isNormal lhs) $ failureLhsReduction lhs
rhs <- etaContract =<< normalise rhs
unless (null $ allMetas (lhs, rhs, b)) failureMetas
pat <- patternFrom 0 lhs
unlessNull (allFreeVars rhs IntSet.\\ nlPatVars pat) failureFreeVars
return $ RewriteRule q gamma pat rhs b
reportSDoc "rewriting" 10 $
text "considering rewrite rule " <+> prettyTCM rew
reportSDoc "rewriting" 60 $
text "considering rewrite rule" <+> text (show rew)
addRewriteRules f [rew]
_ -> failureWrongTarget
where
normaliseArgs :: Term -> TCM Term
normaliseArgs v = case ignoreSharing v of
Def f es -> Def f <$> do etaContract =<< normalise es
Con c vs -> Con c <$> do etaContract =<< normalise vs
_ -> __IMPOSSIBLE__
isNormal :: Term -> TCM Bool
isNormal v = do
v' <- normalise v
return $ v == v'
addRewriteRules :: QName -> RewriteRules -> TCM ()
addRewriteRules f rews = do
reportSDoc "rewriting" 10 $ text "rewrite rule ok, adding it to the definition of " <+> prettyTCM f
modifySignature $ addRewriteRulesFor f rews
rewriteWith :: Maybe Type -> Term -> RewriteRule -> ReduceM (Either (Blocked Term) Term)
rewriteWith mt v rew@(RewriteRule q gamma lhs rhs b) = do
Red.traceSDoc "rewriting" 75 (sep
[ text "attempting to rewrite term " <+> prettyTCM v
, text " with rule " <+> prettyTCM rew
]) $ do
result <- nonLinMatch gamma lhs v
case result of
Left block -> return $ Left $ const v <$> block
Right sub -> do
let v' = applySubst sub rhs
Red.traceSDoc "rewriting" 70 (sep
[ text "rewrote " <+> prettyTCM v
, text " to " <+> prettyTCM v'
]) $ return $ Right v'
rewrite :: Blocked Term -> ReduceM (Either (Blocked Term) Term)
rewrite bv = ifNotM (optRewriting <$> pragmaOptions) (return $ Left bv) $ do
let v = ignoreBlocking bv
case ignoreSharing v of
Def f es -> rew f (Def f) es
Con c vs -> rew (conName c) hd (Apply <$> vs)
where hd es = Con c $ fromMaybe __IMPOSSIBLE__ $ allApplyElims es
_ -> return $ Left bv
where
rew :: QName -> (Elims -> Term) -> Elims -> ReduceM (Either (Blocked Term) Term)
rew f hd es = do
rules <- getRewriteRulesFor f
case rules of
[] -> return $ Left $ bv $> hd es
_ -> do
es <- instantiateFull' es
loop (void bv) es rules
where
loop :: Blocked_ -> Elims -> RewriteRules -> ReduceM (Either (Blocked Term) Term)
loop block es [] = return $ Left $ block $> hd es
loop block es (rew:rews)
| let n = rewArity rew, length es >= n = do
let (es1, es2) = List.genericSplitAt n es
result <- rewriteWith Nothing (hd es1) rew
case result of
Left (Blocked m u) -> loop (block `mappend` Blocked m ()) es rews
Left (NotBlocked _ _) -> loop block es rews
Right w -> return $ Right $ w `applyE` es2
| otherwise = loop (block `mappend` NotBlocked Underapplied ()) es rews
class NLPatVars a where
nlPatVars :: a -> IntSet
instance (Foldable f, NLPatVars a) => NLPatVars (f a) where
nlPatVars = foldMap nlPatVars
instance NLPatVars NLPat where
nlPatVars p =
case p of
PVar _ i -> singleton i
PDef _ es -> nlPatVars es
PWild -> empty
PLam _ p' -> nlPatVars $ unAbs p'
PPi a b -> nlPatVars a `IntSet.union` nlPatVars (unAbs b)
PBoundVar _ es -> nlPatVars es
PTerm{} -> empty
rewArity :: RewriteRule -> Int
rewArity rew = case rewLHS rew of
PDef _f es -> length es
_ -> __IMPOSSIBLE__
class KillCtxId a where
killCtxId :: a -> a
instance (Functor f, KillCtxId a) => KillCtxId (f a) where
killCtxId = fmap killCtxId
instance KillCtxId RewriteRule where
killCtxId rule@RewriteRule{ rewLHS = lhs } = rule{ rewLHS = killCtxId lhs }
instance KillCtxId NLPat where
killCtxId p = case p of
PVar _ i -> PVar Nothing i
PWild -> p
PDef f es -> PDef f $ killCtxId es
PLam i x -> PLam i $ killCtxId x
PPi a b -> PPi (killCtxId a) (killCtxId b)
PBoundVar i es -> PBoundVar i $ killCtxId es
PTerm _ -> p