{-# LANGUAGE PatternSynonyms #-}

{-| Functions for inserting implicit arguments at the right places.
-}
module Agda.TypeChecking.Implicit where

import Control.Monad

import Agda.Syntax.Common
import Agda.Syntax.Internal as I

import Agda.TypeChecking.Irrelevance
import {-# SOURCE #-} Agda.TypeChecking.MetaVars
import {-# SOURCE #-} Agda.TypeChecking.Rules.Term (unquoteTactic)
import Agda.TypeChecking.Monad
import Agda.TypeChecking.Reduce
import Agda.TypeChecking.Substitute
import Agda.TypeChecking.Pretty

import Agda.Utils.Functor
import Agda.Utils.Maybe
import Agda.Utils.Tuple

-- | @implicitArgs n expand t@ generates up to @n@ implicit argument
--   metas (unbounded if @n<0@), as long as @t@ is a function type
--   and @expand@ holds on the hiding info of its domain.

implicitArgs
  :: (MonadReduce m, MonadMetaSolver m, MonadDebug m, MonadTCM m)
  => Int               -- ^ @n@, the maximum number of implicts to be inserted.
  -> (Hiding -> Bool)  -- ^ @expand@, the predicate to test whether we should keep inserting.
  -> Type              -- ^ The (function) type @t@ we are eliminating.
  -> m (Args, Type)  -- ^ The eliminating arguments and the remaining type.
implicitArgs :: Int -> (Hiding -> Bool) -> Type -> m (Args, Type)
implicitArgs Int
n Hiding -> Bool
expand Type
t = ([Arg (Named NamedName Term)] -> Args)
-> ([Arg (Named NamedName Term)], Type) -> (Args, Type)
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst ((Arg (Named NamedName Term) -> Arg Term)
-> [Arg (Named NamedName Term)] -> Args
forall a b. (a -> b) -> [a] -> [b]
map ((Named NamedName Term -> Term)
-> Arg (Named NamedName Term) -> Arg Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Named NamedName Term -> Term
forall name a. Named name a -> a
namedThing)) (([Arg (Named NamedName Term)], Type) -> (Args, Type))
-> m ([Arg (Named NamedName Term)], Type) -> m (Args, Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
  Int
-> (Hiding -> ArgName -> Bool)
-> Type
-> m ([Arg (Named NamedName Term)], Type)
forall (m :: * -> *).
(MonadReduce m, MonadMetaSolver m, MonadDebug m, MonadTCM m) =>
Int
-> (Hiding -> ArgName -> Bool)
-> Type
-> m ([Arg (Named NamedName Term)], Type)
implicitNamedArgs Int
n (\ Hiding
h ArgName
x -> Hiding -> Bool
expand Hiding
h) Type
t

-- | @implicitNamedArgs n expand t@ generates up to @n@ named implicit arguments
--   metas (unbounded if @n<0@), as long as @t@ is a function type
--   and @expand@ holds on the hiding and name info of its domain.

implicitNamedArgs
  :: (MonadReduce m, MonadMetaSolver m, MonadDebug m, MonadTCM m)
  => Int                          -- ^ @n@, the maximum number of implicts to be inserted.
  -> (Hiding -> ArgName -> Bool)  -- ^ @expand@, the predicate to test whether we should keep inserting.
  -> Type                         -- ^ The (function) type @t@ we are eliminating.
  -> m (NamedArgs, Type)        -- ^ The eliminating arguments and the remaining type.
implicitNamedArgs :: Int
-> (Hiding -> ArgName -> Bool)
-> Type
-> m ([Arg (Named NamedName Term)], Type)
implicitNamedArgs Int
0 Hiding -> ArgName -> Bool
expand Type
t0 = ([Arg (Named NamedName Term)], Type)
-> m ([Arg (Named NamedName Term)], Type)
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Type
t0)
implicitNamedArgs Int
n Hiding -> ArgName -> Bool
expand Type
t0 = do
    Type
t0' <- Type -> m Type
forall a (m :: * -> *). (Reduce a, MonadReduce m) => a -> m a
reduce Type
t0
    ArgName -> Int -> TCM Doc -> m ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> Int -> TCM Doc -> m ()
reportSDoc ArgName
"tc.term.args" Int
30 (TCM Doc -> m ()) -> TCM Doc -> m ()
forall a b. (a -> b) -> a -> b
$ TCM Doc
"implicitNamedArgs" TCM Doc -> TCM Doc -> TCM Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Type -> TCM Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Type
t0'
    ArgName -> Int -> TCM Doc -> m ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> Int -> TCM Doc -> m ()
reportSDoc ArgName
"tc.term.args" Int
80 (TCM Doc -> m ()) -> TCM Doc -> m ()
forall a b. (a -> b) -> a -> b
$ TCM Doc
"implicitNamedArgs" TCM Doc -> TCM Doc -> TCM Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ArgName -> TCM Doc
forall (m :: * -> *). Monad m => ArgName -> m Doc
text (Type -> ArgName
forall a. Show a => a -> ArgName
show Type
t0')
    case Type -> Term
forall t a. Type'' t a -> a
unEl Type
t0' of
      Pi dom :: Dom Type
dom@Dom{domInfo :: forall t e. Dom' t e -> ArgInfo
domInfo = ArgInfo
info, domTactic :: forall t e. Dom' t e -> Maybe t
domTactic = Maybe Term
tac, unDom :: forall t e. Dom' t e -> e
unDom = Type
a} Abs Type
b
        | let x :: ArgName
x = ArgName -> Dom Type -> ArgName
forall a. LensNamed NamedName a => ArgName -> a -> ArgName
bareNameWithDefault ArgName
"_" Dom Type
dom, Hiding -> ArgName -> Bool
expand (ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
info) ArgName
x -> do
          ArgInfo
info' <- if ArgInfo -> Bool
forall a. LensHiding a => a -> Bool
hidden ArgInfo
info then ArgInfo -> m ArgInfo
forall (m :: * -> *) a. Monad m => a -> m a
return ArgInfo
info else do
            ArgName -> Int -> TCM Doc -> m ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> Int -> TCM Doc -> m ()
reportSDoc ArgName
"tc.term.args.ifs" Int
15 (TCM Doc -> m ()) -> TCM Doc -> m ()
forall a b. (a -> b) -> a -> b
$
              TCM Doc
"inserting instance meta for type" TCM Doc -> TCM Doc -> TCM Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> Type -> TCM Doc
forall a (m :: * -> *). (PrettyTCM a, MonadPretty m) => a -> m Doc
prettyTCM Type
a
            ArgName -> Int -> TCM Doc -> m ()
forall (m :: * -> *).
MonadDebug m =>
ArgName -> Int -> TCM Doc -> m ()
reportSDoc ArgName
"tc.term.args.ifs" Int
40 (TCM Doc -> m ()) -> TCM Doc -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> TCM Doc -> TCM Doc
forall (m :: * -> *). Functor m => Int -> m Doc -> m Doc
nest Int
2 (TCM Doc -> TCM Doc) -> TCM Doc -> TCM Doc
forall a b. (a -> b) -> a -> b
$ [TCM Doc] -> TCM Doc
forall (m :: * -> *). Monad m => [m Doc] -> m Doc
vcat
              [ TCM Doc
"x      = " TCM Doc -> TCM Doc -> TCM Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ArgName -> TCM Doc
forall (m :: * -> *). Monad m => ArgName -> m Doc
text (ArgName -> ArgName
forall a. Show a => a -> ArgName
show ArgName
x)
              , TCM Doc
"hiding = " TCM Doc -> TCM Doc -> TCM Doc
forall (m :: * -> *). Applicative m => m Doc -> m Doc -> m Doc
<+> ArgName -> TCM Doc
forall (m :: * -> *). Monad m => ArgName -> m Doc
text (Hiding -> ArgName
forall a. Show a => a -> ArgName
show (Hiding -> ArgName) -> Hiding -> ArgName
forall a b. (a -> b) -> a -> b
$ ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
info)
              ]

            ArgInfo -> m ArgInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgInfo -> m ArgInfo) -> ArgInfo -> m ArgInfo
forall a b. (a -> b) -> a -> b
$ ArgInfo -> ArgInfo
forall a. LensHiding a => a -> a
makeInstance ArgInfo
info
          (MetaId
_, Term
v) <- ArgInfo -> ArgName -> Comparison -> Type -> m (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
ArgInfo -> ArgName -> Comparison -> Type -> m (MetaId, Term)
newMetaArg ArgInfo
info' ArgName
x Comparison
CmpLeq Type
a
          Maybe Term -> (Term -> m ()) -> m ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust Maybe Term
tac ((Term -> m ()) -> m ()) -> (Term -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Term
tac -> TCM () -> m ()
forall (tcm :: * -> *) a. MonadTCM tcm => TCM a -> tcm a
liftTCM (TCM () -> m ()) -> TCM () -> m ()
forall a b. (a -> b) -> a -> b
$ Term -> Term -> Type -> TCM ()
unquoteTactic Term
tac Term
v Type
a
          let narg :: Arg (Named NamedName Term)
narg = ArgInfo -> Named NamedName Term -> Arg (Named NamedName Term)
forall e. ArgInfo -> e -> Arg e
Arg ArgInfo
info (Maybe NamedName -> Term -> Named NamedName Term
forall name a. Maybe name -> a -> Named name a
Named (NamedName -> Maybe NamedName
forall a. a -> Maybe a
Just (NamedName -> Maybe NamedName) -> NamedName -> Maybe NamedName
forall a b. (a -> b) -> a -> b
$ Origin -> Ranged ArgName -> NamedName
forall a. Origin -> a -> WithOrigin a
WithOrigin Origin
Inserted (Ranged ArgName -> NamedName) -> Ranged ArgName -> NamedName
forall a b. (a -> b) -> a -> b
$ ArgName -> Ranged ArgName
forall a. a -> Ranged a
unranged ArgName
x) Term
v)
          ([Arg (Named NamedName Term)] -> [Arg (Named NamedName Term)])
-> ([Arg (Named NamedName Term)], Type)
-> ([Arg (Named NamedName Term)], Type)
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (Arg (Named NamedName Term)
narg Arg (Named NamedName Term)
-> [Arg (Named NamedName Term)] -> [Arg (Named NamedName Term)]
forall a. a -> [a] -> [a]
:) (([Arg (Named NamedName Term)], Type)
 -> ([Arg (Named NamedName Term)], Type))
-> m ([Arg (Named NamedName Term)], Type)
-> m ([Arg (Named NamedName Term)], Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> (Hiding -> ArgName -> Bool)
-> Type
-> m ([Arg (Named NamedName Term)], Type)
forall (m :: * -> *).
(MonadReduce m, MonadMetaSolver m, MonadDebug m, MonadTCM m) =>
Int
-> (Hiding -> ArgName -> Bool)
-> Type
-> m ([Arg (Named NamedName Term)], Type)
implicitNamedArgs (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Hiding -> ArgName -> Bool
expand (Abs Type -> Term -> Type
forall t a. Subst t a => Abs a -> t -> a
absApp Abs Type
b Term
v)
      Term
_ -> ([Arg (Named NamedName Term)], Type)
-> m ([Arg (Named NamedName Term)], Type)
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Type
t0')

-- | Create a metavariable according to the 'Hiding' info.

newMetaArg
  :: MonadMetaSolver m
  => ArgInfo    -- ^ Kind/relevance of meta.
  -> ArgName    -- ^ Name suggestion for meta.
  -> Comparison -- ^ Check (@CmpLeq@) or infer (@CmpEq@) the type.
  -> Type       -- ^ Type of meta.
  -> m (MetaId, Term)  -- ^ The created meta as id and as term.
newMetaArg :: ArgInfo -> ArgName -> Comparison -> Type -> m (MetaId, Term)
newMetaArg ArgInfo
info ArgName
x Comparison
cmp Type
a = do
  Bool
prp <- Type -> m Bool
forall a (m :: * -> *).
(LensSort a, PrettyTCM a, MonadReduce m, MonadDebug m) =>
a -> m Bool
isPropM Type
a
  let irrelevantIfProp :: m a -> m a
irrelevantIfProp = if Bool
prp then Relevance -> m a -> m a
forall (tcm :: * -> *) r a.
(MonadTCEnv tcm, LensRelevance r) =>
r -> tcm a -> tcm a
applyRelevanceToContext Relevance
Irrelevant else m a -> m a
forall a. a -> a
id
  ArgInfo -> m (MetaId, Term) -> m (MetaId, Term)
forall (tcm :: * -> *) m a.
(MonadTCEnv tcm, LensModality m) =>
m -> tcm a -> tcm a
applyModalityToContext ArgInfo
info (m (MetaId, Term) -> m (MetaId, Term))
-> m (MetaId, Term) -> m (MetaId, Term)
forall a b. (a -> b) -> a -> b
$ m (MetaId, Term) -> m (MetaId, Term)
forall a. m a -> m a
irrelevantIfProp (m (MetaId, Term) -> m (MetaId, Term))
-> m (MetaId, Term) -> m (MetaId, Term)
forall a b. (a -> b) -> a -> b
$
    Hiding -> ArgName -> Type -> m (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
Hiding -> ArgName -> Type -> m (MetaId, Term)
newMeta (ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
info) (ArgName -> ArgName
argNameToString ArgName
x) Type
a
  where
    newMeta :: MonadMetaSolver m => Hiding -> String -> Type -> m (MetaId, Term)
    newMeta :: Hiding -> ArgName -> Type -> m (MetaId, Term)
newMeta Instance{} ArgName
n = ArgName -> Type -> m (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
ArgName -> Type -> m (MetaId, Term)
newInstanceMeta ArgName
n
    newMeta Hiding
Hidden     ArgName
n = RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
newNamedValueMeta RunMetaOccursCheck
RunMetaOccursCheck ArgName
n Comparison
cmp
    newMeta Hiding
NotHidden  ArgName
n = RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
newNamedValueMeta RunMetaOccursCheck
RunMetaOccursCheck ArgName
n Comparison
cmp

-- | Create a questionmark according to the 'Hiding' info.

newInteractionMetaArg
  :: ArgInfo    -- ^ Kind/relevance of meta.
  -> ArgName    -- ^ Name suggestion for meta.
  -> Comparison -- ^ Check (@CmpLeq@) or infer (@CmpEq@) the type.
  -> Type       -- ^ Type of meta.
  -> TCM (MetaId, Term)  -- ^ The created meta as id and as term.
newInteractionMetaArg :: ArgInfo -> ArgName -> Comparison -> Type -> TCM (MetaId, Term)
newInteractionMetaArg ArgInfo
info ArgName
x Comparison
cmp Type
a = do
  ArgInfo -> TCM (MetaId, Term) -> TCM (MetaId, Term)
forall (tcm :: * -> *) m a.
(MonadTCEnv tcm, LensModality m) =>
m -> tcm a -> tcm a
applyModalityToContext ArgInfo
info (TCM (MetaId, Term) -> TCM (MetaId, Term))
-> TCM (MetaId, Term) -> TCM (MetaId, Term)
forall a b. (a -> b) -> a -> b
$
    Hiding -> ArgName -> Type -> TCM (MetaId, Term)
newMeta (ArgInfo -> Hiding
forall a. LensHiding a => a -> Hiding
getHiding ArgInfo
info) (ArgName -> ArgName
argNameToString ArgName
x) Type
a
  where
    newMeta :: Hiding -> String -> Type -> TCM (MetaId, Term)
    newMeta :: Hiding -> ArgName -> Type -> TCM (MetaId, Term)
newMeta Instance{} ArgName
n = ArgName -> Type -> TCM (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
ArgName -> Type -> m (MetaId, Term)
newInstanceMeta ArgName
n
    newMeta Hiding
Hidden     ArgName
n = RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> TCM (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
newNamedValueMeta' RunMetaOccursCheck
RunMetaOccursCheck ArgName
n Comparison
cmp
    newMeta Hiding
NotHidden  ArgName
n = RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> TCM (MetaId, Term)
forall (m :: * -> *).
MonadMetaSolver m =>
RunMetaOccursCheck
-> ArgName -> Comparison -> Type -> m (MetaId, Term)
newNamedValueMeta' RunMetaOccursCheck
RunMetaOccursCheck ArgName
n Comparison
cmp

---------------------------------------------------------------------------

-- | Possible results of 'insertImplicit'.
data ImplicitInsertion
      = ImpInsert [Dom ()] -- ^ Success: this many implicits have to be inserted (list can be empty).
      | BadImplicits       -- ^ Error: hidden argument where there should have been a non-hidden argument.
      | NoSuchName ArgName -- ^ Error: bad named argument.
  deriving (Int -> ImplicitInsertion -> ArgName -> ArgName
[ImplicitInsertion] -> ArgName -> ArgName
ImplicitInsertion -> ArgName
(Int -> ImplicitInsertion -> ArgName -> ArgName)
-> (ImplicitInsertion -> ArgName)
-> ([ImplicitInsertion] -> ArgName -> ArgName)
-> Show ImplicitInsertion
forall a.
(Int -> a -> ArgName -> ArgName)
-> (a -> ArgName) -> ([a] -> ArgName -> ArgName) -> Show a
showList :: [ImplicitInsertion] -> ArgName -> ArgName
$cshowList :: [ImplicitInsertion] -> ArgName -> ArgName
show :: ImplicitInsertion -> ArgName
$cshow :: ImplicitInsertion -> ArgName
showsPrec :: Int -> ImplicitInsertion -> ArgName -> ArgName
$cshowsPrec :: Int -> ImplicitInsertion -> ArgName -> ArgName
Show)

pattern NoInsertNeeded :: ImplicitInsertion
pattern $bNoInsertNeeded :: ImplicitInsertion
$mNoInsertNeeded :: forall r. ImplicitInsertion -> (Void# -> r) -> (Void# -> r) -> r
NoInsertNeeded = ImpInsert []

-- | If the next given argument is @a@ and the expected arguments are @ts@
--   @insertImplicit' a ts@ returns the prefix of @ts@ that precedes @a@.
--
--   If @a@ is named but this name does not appear in @ts@, the 'NoSuchName' exception is thrown.
--
insertImplicit
  :: NamedArg e  -- ^ Next given argument @a@.
  -> [Dom a]     -- ^ Expected arguments @ts@.
  -> ImplicitInsertion
insertImplicit :: NamedArg e -> [Dom a] -> ImplicitInsertion
insertImplicit NamedArg e
a [Dom a]
doms = NamedArg e -> [Dom ArgName] -> ImplicitInsertion
forall e. NamedArg e -> [Dom ArgName] -> ImplicitInsertion
insertImplicit' NamedArg e
a ([Dom ArgName] -> ImplicitInsertion)
-> [Dom ArgName] -> ImplicitInsertion
forall a b. (a -> b) -> a -> b
$
  [Dom a] -> (Dom a -> Dom ArgName) -> [Dom ArgName]
forall (m :: * -> *) a b. Functor m => m a -> (a -> b) -> m b
for [Dom a]
doms ((Dom a -> Dom ArgName) -> [Dom ArgName])
-> (Dom a -> Dom ArgName) -> [Dom ArgName]
forall a b. (a -> b) -> a -> b
$ \ Dom a
dom ->
    Dom a
dom Dom a -> ArgName -> Dom ArgName
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> ArgName -> Dom a -> ArgName
forall a. LensNamed NamedName a => ArgName -> a -> ArgName
bareNameWithDefault ArgName
"_" Dom a
dom

-- | If the next given argument is @a@ and the expected arguments are @ts@
--   @insertImplicit' a ts@ returns the prefix of @ts@ that precedes @a@.
--
--   If @a@ is named but this name does not appear in @ts@, the 'NoSuchName' exception is thrown.
--
insertImplicit'
  :: NamedArg e     -- ^ Next given argument @a@.
  -> [Dom ArgName]  -- ^ Expected arguments @ts@.
  -> ImplicitInsertion
insertImplicit' :: NamedArg e -> [Dom ArgName] -> ImplicitInsertion
insertImplicit' NamedArg e
_ [] = ImplicitInsertion
BadImplicits
insertImplicit' NamedArg e
a [Dom ArgName]
ts

  -- If @a@ is visible, then take the non-visible prefix of @ts@.
  | NamedArg e -> Bool
forall a. LensHiding a => a -> Bool
visible NamedArg e
a = [Dom ()] -> ImplicitInsertion
ImpInsert ([Dom ()] -> ImplicitInsertion) -> [Dom ()] -> ImplicitInsertion
forall a b. (a -> b) -> a -> b
$ (Dom () -> Bool) -> [Dom ()] -> [Dom ()]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile Dom () -> Bool
forall a. LensHiding a => a -> Bool
notVisible ([Dom ()] -> [Dom ()]) -> [Dom ()] -> [Dom ()]
forall a b. (a -> b) -> a -> b
$ (Dom ArgName -> Dom ()) -> [Dom ArgName] -> [Dom ()]
forall a b. (a -> b) -> [a] -> [b]
map Dom ArgName -> Dom ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void [Dom ArgName]
ts

  -- If @a@ is named, take prefix of @ts@ until the name of @a@ (with correct hiding).
  -- If the name is not found, throw exception 'NoSuchName'.
  | Just ArgName
x <- NamedArg e -> Maybe ArgName
forall a. LensNamed NamedName a => a -> Maybe ArgName
bareNameOf NamedArg e
a = ImplicitInsertion
-> ([Dom ()] -> ImplicitInsertion)
-> Maybe [Dom ()]
-> ImplicitInsertion
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ArgName -> ImplicitInsertion
NoSuchName ArgName
x) [Dom ()] -> ImplicitInsertion
ImpInsert (Maybe [Dom ()] -> ImplicitInsertion)
-> Maybe [Dom ()] -> ImplicitInsertion
forall a b. (a -> b) -> a -> b
$
      (Dom ArgName -> Bool) -> [Dom ArgName] -> Maybe [Dom ()]
takeHiddenUntil (\ Dom ArgName
t -> ArgName
x ArgName -> ArgName -> Bool
forall a. Eq a => a -> a -> Bool
== Dom ArgName -> ArgName
forall t e. Dom' t e -> e
unDom Dom ArgName
t Bool -> Bool -> Bool
&& NamedArg e -> Dom ArgName -> Bool
forall a b. (LensHiding a, LensHiding b) => a -> b -> Bool
sameHiding NamedArg e
a Dom ArgName
t) [Dom ArgName]
ts

  -- If @a@ is neither visible nor named, take prefix of @ts@ with different hiding than @a@.
  | Bool
otherwise = ImplicitInsertion
-> ([Dom ()] -> ImplicitInsertion)
-> Maybe [Dom ()]
-> ImplicitInsertion
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ImplicitInsertion
BadImplicits [Dom ()] -> ImplicitInsertion
ImpInsert (Maybe [Dom ()] -> ImplicitInsertion)
-> Maybe [Dom ()] -> ImplicitInsertion
forall a b. (a -> b) -> a -> b
$
      (Dom ArgName -> Bool) -> [Dom ArgName] -> Maybe [Dom ()]
takeHiddenUntil (NamedArg e -> Dom ArgName -> Bool
forall a b. (LensHiding a, LensHiding b) => a -> b -> Bool
sameHiding NamedArg e
a) [Dom ArgName]
ts

    where
    -- | @takeHiddenUntil p ts@ returns the 'getHiding' of the prefix of @ts@
    --   until @p@ holds or a visible argument is encountered.
    --   If @p@ never holds, 'Nothing' is returned.
    --
    --   Precondition: @p@ should imply @not . visible@.
    takeHiddenUntil :: (Dom ArgName -> Bool) -> [Dom ArgName] -> Maybe [Dom ()]
    takeHiddenUntil :: (Dom ArgName -> Bool) -> [Dom ArgName] -> Maybe [Dom ()]
takeHiddenUntil Dom ArgName -> Bool
p [Dom ArgName]
ts =
      case [Dom ArgName]
ts2 of
        []      -> Maybe [Dom ()]
forall a. Maybe a
Nothing  -- Predicate was never true
        (Dom ArgName
t : [Dom ArgName]
_) -> if Dom ArgName -> Bool
forall a. LensHiding a => a -> Bool
visible Dom ArgName
t then Maybe [Dom ()]
forall a. Maybe a
Nothing else [Dom ()] -> Maybe [Dom ()]
forall a. a -> Maybe a
Just ([Dom ()] -> Maybe [Dom ()]) -> [Dom ()] -> Maybe [Dom ()]
forall a b. (a -> b) -> a -> b
$ (Dom ArgName -> Dom ()) -> [Dom ArgName] -> [Dom ()]
forall a b. (a -> b) -> [a] -> [b]
map Dom ArgName -> Dom ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void [Dom ArgName]
ts1
      where
      ([Dom ArgName]
ts1, [Dom ArgName]
ts2) = (Dom ArgName -> Bool)
-> [Dom ArgName] -> ([Dom ArgName], [Dom ArgName])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (\ Dom ArgName
t -> Dom ArgName -> Bool
p Dom ArgName
t Bool -> Bool -> Bool
|| Dom ArgName -> Bool
forall a. LensHiding a => a -> Bool
visible Dom ArgName
t) [Dom ArgName]
ts