module Agda.TypeChecking.Forcing
( computeForcingAnnotations,
isForced,
nextIsForced ) where
import Data.Monoid
import Agda.Interaction.Options
import Agda.Syntax.Common
import Agda.Syntax.Internal
import Agda.TypeChecking.Monad
import Agda.TypeChecking.Reduce
import Agda.TypeChecking.Substitute
import Agda.TypeChecking.Telescope
import Agda.Utils.List
import Agda.Utils.Monad
import Agda.Utils.Pretty (prettyShow)
import Agda.Utils.Size
import Agda.Utils.Impossible
computeForcingAnnotations :: QName -> Type -> TCM [IsForced]
computeForcingAnnotations :: QName -> Type -> TCM [IsForced]
computeForcingAnnotations QName
c Type
t =
TCMT IO Bool -> TCM [IsForced] -> TCM [IsForced] -> TCM [IsForced]
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifNotM (PragmaOptions -> Bool
optForcing (PragmaOptions -> Bool) -> TCMT IO PragmaOptions -> TCMT IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TCMT IO PragmaOptions
forall (m :: * -> *). HasOptions m => m PragmaOptions
pragmaOptions ) ([IsForced] -> TCM [IsForced]
forall (m :: * -> *) a. Monad m => a -> m a
return []) (TCM [IsForced] -> TCM [IsForced])
-> TCM [IsForced] -> TCM [IsForced]
forall a b. (a -> b) -> a -> b
$ do
Type
t <- Type -> TCMT IO Type
forall a (m :: * -> *).
(InstantiateFull a, MonadReduce m) =>
a -> m a
instantiateFull Type
t
TelV Tele (Dom Type)
tel (El Sort' Term
_ Term
a) <- Type -> TCMT IO (TelV Type)
forall (m :: * -> *). PureTCM m => Type -> m (TelV Type)
telViewPath Type
t
let vs :: Elims
vs = case Term
a of
Def QName
_ Elims
us -> Elims
us
Term
_ -> Elims
forall a. HasCallStack => a
__IMPOSSIBLE__
n :: Nat
n = Tele (Dom Type) -> Nat
forall a. Sized a => a -> Nat
size Tele (Dom Type)
tel
xs :: [(Modality, Nat)]
xs :: [(Modality, Nat)]
xs = Elims -> [(Modality, Nat)]
forall a. ForcedVariables a => a -> [(Modality, Nat)]
forcedVariables Elims
vs
isForced :: Modality -> Nat -> Bool
isForced :: Modality -> Nat -> Bool
isForced Modality
m Nat
i =
(Modality -> Bool
forall a. LensQuantity a => a -> Bool
hasQuantity0 Modality
m Bool -> Bool -> Bool
|| Modality -> Bool
forall a. LensQuantity a => a -> Bool
noUserQuantity Modality
m)
Bool -> Bool -> Bool
&& (Modality -> Relevance
forall a. LensRelevance a => a -> Relevance
getRelevance Modality
m Relevance -> Relevance -> Bool
forall a. Eq a => a -> a -> Bool
/= Relevance
Irrelevant)
Bool -> Bool -> Bool
&& ((Modality, Nat) -> Bool) -> [(Modality, Nat)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(Modality
m', Nat
j) -> Nat
i Nat -> Nat -> Bool
forall a. Eq a => a -> a -> Bool
== Nat
j
Bool -> Bool -> Bool
&& Modality
m' Modality -> Modality -> Bool
`moreUsableModality` Modality
m) [(Modality, Nat)]
xs
forcedArgs :: [IsForced]
forcedArgs =
[ if Modality -> Nat -> Bool
isForced Modality
m Nat
i then IsForced
Forced else IsForced
NotForced
| (Nat
i, Modality
m) <- [Nat] -> [Modality] -> [(Nat, Modality)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Nat -> [Nat]
forall a. Integral a => a -> [a]
downFrom Nat
n) ([Modality] -> [(Nat, Modality)])
-> [Modality] -> [(Nat, Modality)]
forall a b. (a -> b) -> a -> b
$ (Dom (ArgName, Type) -> Modality)
-> [Dom (ArgName, Type)] -> [Modality]
forall a b. (a -> b) -> [a] -> [b]
map Dom (ArgName, Type) -> Modality
forall a. LensModality a => a -> Modality
getModality (Tele (Dom Type) -> [Dom (ArgName, Type)]
forall t. Tele (Dom t) -> [Dom (ArgName, t)]
telToList Tele (Dom Type)
tel)
]
ArgName -> Nat -> [ArgName] -> TCMT IO ()
forall a (m :: * -> *).
(ReportS a, MonadDebug m) =>
ArgName -> Nat -> a -> m ()
reportS ArgName
"tc.force" Nat
60
[ ArgName
"Forcing analysis for " ArgName -> ArgName -> ArgName
forall a. [a] -> [a] -> [a]
++ QName -> ArgName
forall a. Pretty a => a -> ArgName
prettyShow QName
c
, ArgName
" xs = " ArgName -> ArgName -> ArgName
forall a. [a] -> [a] -> [a]
++ [Nat] -> ArgName
forall a. Show a => a -> ArgName
show (((Modality, Nat) -> Nat) -> [(Modality, Nat)] -> [Nat]
forall a b. (a -> b) -> [a] -> [b]
map (Modality, Nat) -> Nat
forall a b. (a, b) -> b
snd [(Modality, Nat)]
xs)
, ArgName
" forcedArgs = " ArgName -> ArgName -> ArgName
forall a. [a] -> [a] -> [a]
++ [IsForced] -> ArgName
forall a. Show a => a -> ArgName
show [IsForced]
forcedArgs
]
[IsForced] -> TCM [IsForced]
forall (m :: * -> *) a. Monad m => a -> m a
return [IsForced]
forcedArgs
class ForcedVariables a where
forcedVariables :: a -> [(Modality, Nat)]
default forcedVariables :: (ForcedVariables b, Foldable t, a ~ t b) => a -> [(Modality, Nat)]
forcedVariables = (b -> [(Modality, Nat)]) -> t b -> [(Modality, Nat)]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap b -> [(Modality, Nat)]
forall a. ForcedVariables a => a -> [(Modality, Nat)]
forcedVariables
instance ForcedVariables a => ForcedVariables [a] where
instance ForcedVariables a => ForcedVariables (Elim' a) where
forcedVariables :: Elim' a -> [(Modality, Nat)]
forcedVariables (Apply Arg a
x) = Arg a -> [(Modality, Nat)]
forall a. ForcedVariables a => a -> [(Modality, Nat)]
forcedVariables Arg a
x
forcedVariables IApply{} = []
forcedVariables Proj{} = []
instance ForcedVariables a => ForcedVariables (Arg a) where
forcedVariables :: Arg a -> [(Modality, Nat)]
forcedVariables Arg a
x = [ (Modality -> Modality -> Modality
composeModality Modality
m Modality
m', Nat
i) | (Modality
m', Nat
i) <- a -> [(Modality, Nat)]
forall a. ForcedVariables a => a -> [(Modality, Nat)]
forcedVariables (Arg a -> a
forall e. Arg e -> e
unArg Arg a
x) ]
where m :: Modality
m = Arg a -> Modality
forall a. LensModality a => a -> Modality
getModality Arg a
x
instance ForcedVariables Term where
forcedVariables :: Term -> [(Modality, Nat)]
forcedVariables = \case
Var Nat
i [] -> [(Modality
unitModality, Nat
i)]
Con ConHead
_ ConInfo
_ Elims
vs -> Elims -> [(Modality, Nat)]
forall a. ForcedVariables a => a -> [(Modality, Nat)]
forcedVariables Elims
vs
Term
_ -> []
isForced :: IsForced -> Bool
isForced :: IsForced -> Bool
isForced IsForced
Forced = Bool
True
isForced IsForced
NotForced = Bool
False
nextIsForced :: [IsForced] -> (IsForced, [IsForced])
nextIsForced :: [IsForced] -> (IsForced, [IsForced])
nextIsForced [] = (IsForced
NotForced, [])
nextIsForced (IsForced
f:[IsForced]
fs) = (IsForced
f, [IsForced]
fs)