{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998


Matching guarded right-hand-sides (GRHSs)
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE ViewPatterns #-}

module GHC.HsToCore.GuardedRHSs ( dsGuarded, dsGRHSs, isTrueLHsExpr ) where

#include "HsVersions.h"

import GHC.Prelude

import {-# SOURCE #-} GHC.HsToCore.Expr  ( dsLExpr, dsLocalBinds )
import {-# SOURCE #-} GHC.HsToCore.Match ( matchSinglePatVar )

import GHC.Hs
import GHC.Core.Make
import GHC.Core
import GHC.Core.Utils (bindNonRec)

import GHC.HsToCore.Monad
import GHC.HsToCore.Utils
import GHC.HsToCore.PmCheck.Types ( Deltas, initDeltas )
import GHC.Core.Type ( Type )
import GHC.Utils.Misc
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Core.Multiplicity
import Control.Monad ( zipWithM )
import Data.List.NonEmpty ( NonEmpty, toList )

{-
@dsGuarded@ is used for pattern bindings.
It desugars:
\begin{verbatim}
        | g1 -> e1
        ...
        | gn -> en
        where binds
\end{verbatim}
producing an expression with a runtime error in the corner if
necessary.  The type argument gives the type of the @ei@.
-}

dsGuarded :: GRHSs GhcTc (LHsExpr GhcTc) -> Type -> Maybe (NonEmpty Deltas) -> DsM CoreExpr
dsGuarded :: GRHSs GhcTc (LHsExpr GhcTc)
-> Type -> Maybe (NonEmpty Deltas) -> DsM CoreExpr
dsGuarded GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
rhs_ty Maybe (NonEmpty Deltas)
mb_rhss_deltas = do
    MatchResult CoreExpr
match_result <- HsMatchContext GhcRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> Maybe (NonEmpty Deltas)
-> DsM (MatchResult CoreExpr)
dsGRHSs HsMatchContext GhcRn
forall p. HsMatchContext p
PatBindRhs GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
rhs_ty Maybe (NonEmpty Deltas)
mb_rhss_deltas
    CoreExpr
error_expr <- Id -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs Id
nON_EXHAUSTIVE_GUARDS_ERROR_ID Type
rhs_ty SDoc
empty
    MatchResult CoreExpr -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult CoreExpr
match_result CoreExpr
error_expr

-- In contrast, @dsGRHSs@ produces a @MatchResult CoreExpr@.

dsGRHSs :: HsMatchContext GhcRn
        -> GRHSs GhcTc (LHsExpr GhcTc) -- ^ Guarded RHSs
        -> Type                        -- ^ Type of RHS
        -> Maybe (NonEmpty Deltas)     -- ^ Refined pattern match checking
                                       --   models, one for each GRHS. Defaults
                                       --   to 'initDeltas' if 'Nothing'.
        -> DsM (MatchResult CoreExpr)
dsGRHSs :: HsMatchContext GhcRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> Maybe (NonEmpty Deltas)
-> DsM (MatchResult CoreExpr)
dsGRHSs HsMatchContext GhcRn
hs_ctx (GRHSs XCGRHSs GhcTc (LHsExpr GhcTc)
_ [LGRHS GhcTc (LHsExpr GhcTc)]
grhss LHsLocalBinds GhcTc
binds) Type
rhs_ty Maybe (NonEmpty Deltas)
mb_rhss_deltas
  = ASSERT( notNull grhss )
    do { [MatchResult CoreExpr]
match_results <- case NonEmpty Deltas -> [Deltas]
forall a. NonEmpty a -> [a]
toList (NonEmpty Deltas -> [Deltas])
-> Maybe (NonEmpty Deltas) -> Maybe [Deltas]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NonEmpty Deltas)
mb_rhss_deltas of
           Maybe [Deltas]
Nothing          -> (LGRHS GhcTc (LHsExpr GhcTc) -> DsM (MatchResult CoreExpr))
-> [LGRHS GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM     (HsMatchContext GhcRn
-> Type
-> Deltas
-> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS HsMatchContext GhcRn
hs_ctx Type
rhs_ty Deltas
initDeltas) [LGRHS GhcTc (LHsExpr GhcTc)]
grhss
           Just [Deltas]
rhss_deltas -> ASSERT( length grhss == length rhss_deltas )
                               (Deltas
 -> LGRHS GhcTc (LHsExpr GhcTc) -> DsM (MatchResult CoreExpr))
-> [Deltas]
-> [LGRHS GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (HsMatchContext GhcRn
-> Type
-> Deltas
-> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS HsMatchContext GhcRn
hs_ctx Type
rhs_ty) [Deltas]
rhss_deltas [LGRHS GhcTc (LHsExpr GhcTc)]
grhss
       ; let match_result1 :: MatchResult CoreExpr
match_result1 = (MatchResult CoreExpr
 -> MatchResult CoreExpr -> MatchResult CoreExpr)
-> [MatchResult CoreExpr] -> MatchResult CoreExpr
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr
combineMatchResults [MatchResult CoreExpr]
match_results
             match_result2 :: MatchResult CoreExpr
match_result2 = (CoreExpr -> DsM CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall a b. (a -> DsM b) -> MatchResult a -> MatchResult b
adjustMatchResultDs (LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
binds) MatchResult CoreExpr
match_result1
                             -- NB: nested dsLet inside matchResult
       ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return MatchResult CoreExpr
match_result2 }

dsGRHS :: HsMatchContext GhcRn -> Type -> Deltas -> LGRHS GhcTc (LHsExpr GhcTc)
       -> DsM (MatchResult CoreExpr)
dsGRHS :: HsMatchContext GhcRn
-> Type
-> Deltas
-> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS HsMatchContext GhcRn
hs_ctx Type
rhs_ty Deltas
rhs_deltas (L SrcSpan
_ (GRHS XCGRHS GhcTc (LHsExpr GhcTc)
_ [GuardLStmt GhcTc]
guards LHsExpr GhcTc
rhs))
  = Deltas -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. Deltas -> DsM a -> DsM a
updPmDeltas Deltas
rhs_deltas ([GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards ((GuardLStmt GhcTc -> GuardStmt GhcTc)
-> [GuardLStmt GhcTc] -> [GuardStmt GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map GuardLStmt GhcTc -> GuardStmt GhcTc
forall l e. GenLocated l e -> e
unLoc [GuardLStmt GhcTc]
guards) (HsMatchContext GhcRn -> HsStmtContext GhcRn
forall p. HsMatchContext p -> HsStmtContext p
PatGuard HsMatchContext GhcRn
hs_ctx) LHsExpr GhcTc
rhs Type
rhs_ty)

{-
************************************************************************
*                                                                      *
*  matchGuard : make a MatchResult CoreExpr CoreExpr from a guarded RHS                  *
*                                                                      *
************************************************************************
-}

matchGuards :: [GuardStmt GhcTc]     -- Guard
            -> HsStmtContext GhcRn   -- Context
            -> LHsExpr GhcTc         -- RHS
            -> Type                  -- Type of RHS of guard
            -> DsM (MatchResult CoreExpr)

-- See comments with HsExpr.Stmt re what a BodyStmt means
-- Here we must be in a guard context (not do-expression, nor list-comp)

matchGuards :: [GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [] HsStmtContext GhcRn
_ LHsExpr GhcTc
rhs Type
_
  = do  { CoreExpr
core_rhs <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
        ; MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> MatchResult CoreExpr
cantFailMatchResult CoreExpr
core_rhs) }

        -- BodyStmts must be guards
        -- Turn an "otherwise" guard is a no-op.  This ensures that
        -- you don't get a "non-exhaustive eqns" message when the guards
        -- finish in "otherwise".
        -- NB:  The success of this clause depends on the typechecker not
        --      wrapping the 'otherwise' in empty HsTyApp or HsWrap constructors
        --      If it does, you'll get bogus overlap warnings
matchGuards (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
e SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [GuardStmt GhcTc]
stmts) HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty
  | Just CoreExpr -> DsM CoreExpr
addTicks <- LHsExpr GhcTc -> Maybe (CoreExpr -> DsM CoreExpr)
isTrueLHsExpr LHsExpr GhcTc
e = do
    MatchResult CoreExpr
match_result <- [GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty
    MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ((CoreExpr -> DsM CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall a b. (a -> DsM b) -> MatchResult a -> MatchResult b
adjustMatchResultDs CoreExpr -> DsM CoreExpr
addTicks MatchResult CoreExpr
match_result)
matchGuards (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
expr SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [GuardStmt GhcTc]
stmts) HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty = do
    MatchResult CoreExpr
match_result <- [GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty
    CoreExpr
pred_expr <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr
    MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> MatchResult CoreExpr -> MatchResult CoreExpr
mkGuardedMatchResult CoreExpr
pred_expr MatchResult CoreExpr
match_result)

matchGuards (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsLocalBinds GhcTc
binds : [GuardStmt GhcTc]
stmts) HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty = do
    MatchResult CoreExpr
match_result <- [GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty
    MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ((CoreExpr -> DsM CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall a b. (a -> DsM b) -> MatchResult a -> MatchResult b
adjustMatchResultDs (LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
binds) MatchResult CoreExpr
match_result)
        -- NB the dsLet occurs inside the match_result
        -- Reason: dsLet takes the body expression as its argument
        --         so we can't desugar the bindings without the
        --         body expression in hand

matchGuards (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
bind_rhs : [GuardStmt GhcTc]
stmts) HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty = do
    let upat :: Pat GhcTc
upat = GenLocated SrcSpan (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpan (Pat GhcTc)
LPat GhcTc
pat
    Id
match_var <- Type -> Pat GhcTc -> DsM Id
selectMatchVar Type
Many Pat GhcTc
upat
       -- We only allow unrestricted patterns in guard, hence the `Many`
       -- above. It isn't clear what linear patterns would mean, maybe we will
       -- figure it out in the future.

    MatchResult CoreExpr
match_result <- [GuardStmt GhcTc]
-> HsStmtContext GhcRn
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsStmtContext GhcRn
ctx LHsExpr GhcTc
rhs Type
rhs_ty
    CoreExpr
core_rhs <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
bind_rhs
    MatchResult CoreExpr
match_result' <- Id
-> HsMatchContext GhcRn
-> LPat GhcTc
-> Type
-> MatchResult CoreExpr
-> DsM (MatchResult CoreExpr)
matchSinglePatVar Id
match_var (HsStmtContext GhcRn -> HsMatchContext GhcRn
forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt HsStmtContext GhcRn
ctx) LPat GhcTc
pat Type
rhs_ty
                                       MatchResult CoreExpr
match_result
    MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MatchResult CoreExpr -> DsM (MatchResult CoreExpr))
-> MatchResult CoreExpr -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$ Id -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec Id
match_var CoreExpr
core_rhs (CoreExpr -> CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MatchResult CoreExpr
match_result'

matchGuards (LastStmt  {} : [GuardStmt GhcTc]
_) HsStmtContext GhcRn
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. String -> a
panic String
"matchGuards LastStmt"
matchGuards (ParStmt   {} : [GuardStmt GhcTc]
_) HsStmtContext GhcRn
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. String -> a
panic String
"matchGuards ParStmt"
matchGuards (TransStmt {} : [GuardStmt GhcTc]
_) HsStmtContext GhcRn
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. String -> a
panic String
"matchGuards TransStmt"
matchGuards (RecStmt   {} : [GuardStmt GhcTc]
_) HsStmtContext GhcRn
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. String -> a
panic String
"matchGuards RecStmt"
matchGuards (ApplicativeStmt {} : [GuardStmt GhcTc]
_) HsStmtContext GhcRn
_ LHsExpr GhcTc
_ Type
_ =
  String -> DsM (MatchResult CoreExpr)
forall a. String -> a
panic String
"matchGuards ApplicativeLastStmt"

{-
Should {\em fail} if @e@ returns @D@
\begin{verbatim}
f x | p <- e', let C y# = e, f y# = r1
    | otherwise          = r2
\end{verbatim}
-}