{-# LANGUAGE RankNTypes     #-}
{-# LANGUAGE TypeFamilies   #-}
{-# LANGUAGE BlockArguments #-}

{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}

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

-}

-- | Typecheck arrow notation
module GHC.Tc.Gen.Arrow ( tcProc ) where

import GHC.Prelude

import {-# SOURCE #-}   GHC.Tc.Gen.Expr( tcCheckMonoExpr, tcInferRho, tcSyntaxOp
                                       , tcCheckPolyExpr )

import GHC.Hs
import GHC.Tc.Errors.Types
import GHC.Tc.Gen.Match
import GHC.Tc.Gen.Head( tcCheckId )
import GHC.Tc.Utils.Concrete ( hasFixedRuntimeRep_syntactic )
import GHC.Tc.Utils.TcType
import GHC.Tc.Utils.TcMType
import GHC.Tc.Gen.Bind
import GHC.Tc.Gen.Pat
import GHC.Tc.Utils.Unify
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Env
import GHC.Tc.Types.Origin
import GHC.Tc.Types.Evidence
import GHC.Core.Multiplicity
import GHC.Core.Coercion
import GHC.Types.Id( mkLocalId )
import GHC.Tc.Utils.Instantiate
import GHC.Builtin.Types
import GHC.Types.Var.Set
import GHC.Builtin.Types.Prim
import GHC.Types.Basic( Arity )
import GHC.Types.Error
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Misc

import Control.Monad

{-
Note [Arrow overview]
~~~~~~~~~~~~~~~~~~~~~
Here's a summary of arrows and how they typecheck.  First, here's
a cut-down syntax:

  expr ::= ....
        |  proc pat cmd

  cmd ::= cmd exp                    -- Arrow application
       |  \pat -> cmd                -- Arrow abstraction
       |  (| exp cmd1 ... cmdn |)    -- Arrow form, n>=0
       |  ... -- If, case in the usual way

  cmd_type ::= carg_type --> type

  carg_type ::= ()
             |  (type, carg_type)

Note that
 * The 'exp' in an arrow form can mention only
   "arrow-local" variables

 * An "arrow-local" variable is bound by an enclosing
   cmd binding form (eg arrow abstraction)

 * A cmd_type is here written with a funny arrow "-->",
   The bit on the left is a carg_type (command argument type)
   which itself is a nested tuple, finishing with ()

 * The arrow-tail operator (e1 -< e2) means
       (| e1 <<< arr snd |) e2


************************************************************************
*                                                                      *
                Proc
*                                                                      *
************************************************************************
-}

tcProc :: LPat GhcRn -> LHsCmdTop GhcRn         -- proc pat -> expr
       -> ExpRhoType                            -- Expected type of whole proc expression
       -> TcM (LPat GhcTc, LHsCmdTop GhcTc, TcCoercion)

tcProc :: LPat GhcRn
-> LHsCmdTop GhcRn
-> ExpRhoType
-> TcM (LPat GhcTc, LHsCmdTop GhcTc, TcCoercion)
tcProc LPat GhcRn
pat cmd :: LHsCmdTop GhcRn
cmd@(L SrcAnn NoEpAnns
loc (HsCmdTop XCmdTop GhcRn
names LHsCmd GhcRn
_)) ExpRhoType
exp_ty
  = do  { TcType
exp_ty <- ExpRhoType -> TcM TcType
expTypeToType ExpRhoType
exp_ty  -- no higher-rank stuff with arrows
        ; (TcCoercion
co, (TcType
exp_ty1, TcType
res_ty)) <- TcType -> TcM (TcCoercion, (TcType, TcType))
matchExpectedAppTy TcType
exp_ty
        ; (TcCoercion
co1, (TcType
arr_ty, TcType
arg_ty)) <- TcType -> TcM (TcCoercion, (TcType, TcType))
matchExpectedAppTy TcType
exp_ty1
        -- start with the names as they are used to desugar the proc itself
        -- See #17423
        ; [(Name, HsExpr GhcTc)]
names' <- SrcAnn NoEpAnns
-> TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)]
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc (TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)])
-> TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)]
forall a b. (a -> b) -> a -> b
$
            ((Name, HsExpr GhcRn)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Name, HsExpr GhcTc))
-> [(Name, HsExpr GhcRn)] -> TcRn [(Name, HsExpr GhcTc)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (CtOrigin
-> TcType
-> (Name, HsExpr GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (Name, HsExpr GhcTc)
tcSyntaxName CtOrigin
ProcOrigin TcType
arr_ty) [(Name, HsExpr GhcRn)]
XCmdTop GhcRn
names
        ; let cmd_env :: CmdEnv
cmd_env = CmdEnv { cmd_arr :: TcType
cmd_arr = TcType
arr_ty }
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd') <- TcM
  (GenLocated SrcSpanAnnA (Pat GhcTc),
   GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
-> TcM
     (GenLocated SrcSpanAnnA (Pat GhcTc),
      GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
forall a. TcM a -> TcM a
newArrowScope
                          (TcM
   (GenLocated SrcSpanAnnA (Pat GhcTc),
    GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
 -> TcM
      (GenLocated SrcSpanAnnA (Pat GhcTc),
       GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)))
-> TcM
     (GenLocated SrcSpanAnnA (Pat GhcTc),
      GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
-> TcM
     (GenLocated SrcSpanAnnA (Pat GhcTc),
      GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
forall a b. (a -> b) -> a -> b
$ HsMatchContext GhcTc
-> LPat GhcRn
-> Scaled TcType
-> TcM (LHsCmdTop GhcTc)
-> TcM (LPat GhcTc, LHsCmdTop GhcTc)
forall a.
HsMatchContext GhcTc
-> LPat GhcRn -> Scaled TcType -> TcM a -> TcM (LPat GhcTc, a)
tcCheckPat (HsArrowMatchContext -> HsMatchContext GhcTc
forall p. HsArrowMatchContext -> HsMatchContext p
ArrowMatchCtxt HsArrowMatchContext
ProcExpr) LPat GhcRn
pat (TcType -> Scaled TcType
forall a. a -> Scaled a
unrestricted TcType
arg_ty)
                          (TcM (LHsCmdTop GhcTc) -> TcM (LPat GhcTc, LHsCmdTop GhcTc))
-> TcM (LHsCmdTop GhcTc) -> TcM (LPat GhcTc, LHsCmdTop GhcTc)
forall a b. (a -> b) -> a -> b
$ CmdEnv
-> [(Name, HsExpr GhcTc)]
-> LHsCmdTop GhcRn
-> (TcType, TcType)
-> TcM (LHsCmdTop GhcTc)
tcCmdTop CmdEnv
cmd_env [(Name, HsExpr GhcTc)]
names' LHsCmdTop GhcRn
cmd (TcType
unitTy, TcType
res_ty)
        ; let res_co :: TcCoercion
res_co = TcCoercion
co TcCoercion -> TcCoercion -> TcCoercion
`mkTransCo` TcCoercion -> TcCoercion -> TcCoercion
mkAppCo TcCoercion
co1 (TcType -> TcCoercion
mkNomReflCo TcType
res_ty)
        ; (GenLocated SrcSpanAnnA (Pat GhcTc),
 GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcCoercion)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpanAnnA (Pat GhcTc),
      GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcCoercion)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd', TcCoercion
res_co) }

{-
************************************************************************
*                                                                      *
                Commands
*                                                                      *
************************************************************************
-}

-- See Note [Arrow overview]
type CmdType    = (CmdArgType, TcTauType)    -- cmd_type
type CmdArgType = TcTauType                  -- carg_type, a nested tuple

data CmdEnv
  = CmdEnv {
        CmdEnv -> TcType
cmd_arr :: TcType -- ^ Arrow type constructor, of kind *->*->*
    }

mkCmdArrTy :: CmdEnv -> TcTauType -> TcTauType -> TcTauType
mkCmdArrTy :: CmdEnv -> TcType -> TcType -> TcType
mkCmdArrTy CmdEnv
env TcType
t1 TcType
t2 = TcType -> [TcType] -> TcType
mkAppTys (CmdEnv -> TcType
cmd_arr CmdEnv
env) [TcType
t1, TcType
t2]

---------------------------------------
tcCmdTop :: CmdEnv
         -> CmdSyntaxTable GhcTc -- ^ Type-checked Arrow class methods (arr, (>>>), ...)
         -> LHsCmdTop GhcRn
         -> CmdType
         -> TcM (LHsCmdTop GhcTc)

tcCmdTop :: CmdEnv
-> [(Name, HsExpr GhcTc)]
-> LHsCmdTop GhcRn
-> (TcType, TcType)
-> TcM (LHsCmdTop GhcTc)
tcCmdTop CmdEnv
env [(Name, HsExpr GhcTc)]
names (L SrcAnn NoEpAnns
loc (HsCmdTop XCmdTop GhcRn
_names LHsCmd GhcRn
cmd)) cmd_ty :: (TcType, TcType)
cmd_ty@(TcType
cmd_stk, TcType
res_ty)
  = SrcAnn NoEpAnns -> TcM (LHsCmdTop GhcTc) -> TcM (LHsCmdTop GhcTc)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc (TcM (LHsCmdTop GhcTc) -> TcM (LHsCmdTop GhcTc))
-> TcM (LHsCmdTop GhcTc) -> TcM (LHsCmdTop GhcTc)
forall a b. (a -> b) -> a -> b
$
    do  { GenLocated SrcSpanAnnA (HsCmd GhcTc)
cmd' <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
cmd (TcType, TcType)
cmd_ty
        ; GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcAnn NoEpAnns
-> HsCmdTop GhcTc -> GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (HsCmdTop GhcTc -> GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc))
-> HsCmdTop GhcTc -> GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
forall a b. (a -> b) -> a -> b
$ XCmdTop GhcTc -> LHsCmd GhcTc -> HsCmdTop GhcTc
forall p. XCmdTop p -> LHsCmd p -> HsCmdTop p
HsCmdTop (TcType -> TcType -> [(Name, HsExpr GhcTc)] -> CmdTopTc
CmdTopTc TcType
cmd_stk TcType
res_ty [(Name, HsExpr GhcTc)]
names) LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
cmd') }

----------------------------------------
tcCmd  :: CmdEnv -> LHsCmd GhcRn -> CmdType -> TcM (LHsCmd GhcTc)
        -- The main recursive function
tcCmd :: CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env (L SrcSpanAnnA
loc HsCmd GhcRn
cmd) cmd_ty :: (TcType, TcType)
cmd_ty@(TcType
_, TcType
res_ty)
  = SrcSpan -> TcM (LHsCmd GhcTc) -> TcM (LHsCmd GhcTc)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) (TcM (LHsCmd GhcTc) -> TcM (LHsCmd GhcTc))
-> TcM (LHsCmd GhcTc) -> TcM (LHsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$ do
        { HsCmd GhcTc
cmd' <- CmdEnv -> HsCmd GhcRn -> (TcType, TcType) -> TcM (HsCmd GhcTc)
tc_cmd CmdEnv
env HsCmd GhcRn
cmd (TcType, TcType)
cmd_ty
        ; HasDebugCallStack => FixedRuntimeRepContext -> TcType -> TcM ()
FixedRuntimeRepContext -> TcType -> TcM ()
hasFixedRuntimeRep_syntactic (FRRArrowContext -> FixedRuntimeRepContext
FRRArrow (FRRArrowContext -> FixedRuntimeRepContext)
-> FRRArrowContext -> FixedRuntimeRepContext
forall a b. (a -> b) -> a -> b
$ HsCmd GhcRn -> FRRArrowContext
ArrowCmdResTy HsCmd GhcRn
cmd) TcType
res_ty
        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnA -> HsCmd GhcTc -> GenLocated SrcSpanAnnA (HsCmd GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsCmd GhcTc
cmd') }

tc_cmd :: CmdEnv -> HsCmd GhcRn  -> CmdType -> TcM (HsCmd GhcTc)
tc_cmd :: CmdEnv -> HsCmd GhcRn -> (TcType, TcType) -> TcM (HsCmd GhcTc)
tc_cmd CmdEnv
env (HsCmdPar XCmdPar GhcRn
x LHsToken "(" GhcRn
lpar LHsCmd GhcRn
cmd LHsToken ")" GhcRn
rpar) (TcType, TcType)
res_ty
  = do  { GenLocated SrcSpanAnnA (HsCmd GhcTc)
cmd' <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
cmd (TcType, TcType)
res_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdPar GhcTc
-> LHsToken "(" GhcTc
-> LHsCmd GhcTc
-> LHsToken ")" GhcTc
-> HsCmd GhcTc
forall id.
XCmdPar id
-> LHsToken "(" id -> LHsCmd id -> LHsToken ")" id -> HsCmd id
HsCmdPar XCmdPar GhcRn
XCmdPar GhcTc
x LHsToken "(" GhcRn
LHsToken "(" GhcTc
lpar LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
cmd' LHsToken ")" GhcRn
LHsToken ")" GhcTc
rpar) }

tc_cmd CmdEnv
env (HsCmdLet XCmdLet GhcRn
x LHsToken "let" GhcRn
tkLet HsLocalBinds GhcRn
binds LHsToken "in" GhcRn
tkIn (L SrcSpanAnnA
body_loc HsCmd GhcRn
body)) (TcType, TcType)
res_ty
  = do  { (HsLocalBinds GhcTc
binds', HsCmd GhcTc
body') <- HsLocalBinds GhcRn
-> TcM (HsCmd GhcTc) -> TcM (HsLocalBinds GhcTc, HsCmd GhcTc)
forall thing.
HsLocalBinds GhcRn -> TcM thing -> TcM (HsLocalBinds GhcTc, thing)
tcLocalBinds HsLocalBinds GhcRn
binds         (TcM (HsCmd GhcTc) -> TcM (HsLocalBinds GhcTc, HsCmd GhcTc))
-> TcM (HsCmd GhcTc) -> TcM (HsLocalBinds GhcTc, HsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$
                             SrcSpan -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
body_loc) (TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc))
-> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$
                             CmdEnv -> HsCmd GhcRn -> (TcType, TcType) -> TcM (HsCmd GhcTc)
tc_cmd CmdEnv
env HsCmd GhcRn
body (TcType, TcType)
res_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdLet GhcTc
-> LHsToken "let" GhcTc
-> HsLocalBinds GhcTc
-> LHsToken "in" GhcTc
-> LHsCmd GhcTc
-> HsCmd GhcTc
forall id.
XCmdLet id
-> LHsToken "let" id
-> HsLocalBinds id
-> LHsToken "in" id
-> LHsCmd id
-> HsCmd id
HsCmdLet XCmdLet GhcRn
XCmdLet GhcTc
x LHsToken "let" GhcRn
LHsToken "let" GhcTc
tkLet HsLocalBinds GhcTc
binds' LHsToken "in" GhcRn
LHsToken "in" GhcTc
tkIn (SrcSpanAnnA -> HsCmd GhcTc -> GenLocated SrcSpanAnnA (HsCmd GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
body_loc HsCmd GhcTc
body')) }

tc_cmd CmdEnv
env in_cmd :: HsCmd GhcRn
in_cmd@(HsCmdCase XCmdCase GhcRn
x LHsExpr GhcRn
scrut MatchGroup GhcRn (LHsCmd GhcRn)
matches) (TcType
stk, TcType
res_ty)
  = SDoc -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
in_cmd) (TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc))
-> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$ do
      (GenLocated SrcSpanAnnA (HsExpr GhcTc)
scrut', TcType
scrut_ty) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcType)
tcInferRho LHsExpr GhcRn
scrut
      HasDebugCallStack => FixedRuntimeRepContext -> TcType -> TcM ()
FixedRuntimeRepContext -> TcType -> TcM ()
hasFixedRuntimeRep_syntactic
        (FRRArrowContext -> FixedRuntimeRepContext
FRRArrow (FRRArrowContext -> FixedRuntimeRepContext)
-> FRRArrowContext -> FixedRuntimeRepContext
forall a b. (a -> b) -> a -> b
$ FRRArrowContext
ArrowCmdCase)
        TcType
scrut_ty
      MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
matches' <- CmdEnv
-> TcType
-> MatchGroup GhcRn (LHsCmd GhcRn)
-> (TcType, TcType)
-> TcM (MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatches CmdEnv
env TcType
scrut_ty MatchGroup GhcRn (LHsCmd GhcRn)
matches (TcType
stk, TcType
res_ty)
      HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdCase GhcTc
-> LHsExpr GhcTc -> MatchGroup GhcTc (LHsCmd GhcTc) -> HsCmd GhcTc
forall id.
XCmdCase id -> LHsExpr id -> MatchGroup id (LHsCmd id) -> HsCmd id
HsCmdCase XCmdCase GhcRn
XCmdCase GhcTc
x LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
scrut' MatchGroup GhcTc (LHsCmd GhcTc)
MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
matches')

tc_cmd CmdEnv
env cmd :: HsCmd GhcRn
cmd@(HsCmdLamCase XCmdLamCase GhcRn
x LamCaseVariant
lc_variant MatchGroup GhcRn (LHsCmd GhcRn)
match) (TcType, TcType)
cmd_ty
  = SDoc -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
cmd)
      do { let match_ctxt :: HsArrowMatchContext
match_ctxt = LamCaseVariant -> HsArrowMatchContext
ArrowLamCaseAlt LamCaseVariant
lc_variant
         ; HsMatchContext GhcTc
-> MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> TcM ()
forall (body :: * -> *).
AnnoBody body =>
HsMatchContext GhcTc
-> MatchGroup GhcRn (LocatedA (body GhcRn)) -> TcM ()
checkArgCounts (HsArrowMatchContext -> HsMatchContext GhcTc
forall p. HsArrowMatchContext -> HsMatchContext p
ArrowMatchCtxt HsArrowMatchContext
match_ctxt) MatchGroup GhcRn (LHsCmd GhcRn)
MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
match
         ; (HsWrapper
wrap, MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
match') <-
             CmdEnv
-> HsArrowMatchContext
-> MatchGroup GhcRn (LHsCmd GhcRn)
-> (TcType, TcType)
-> TcM (HsWrapper, MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatchLambda CmdEnv
env HsArrowMatchContext
match_ctxt MatchGroup GhcRn (LHsCmd GhcRn)
match (TcType, TcType)
cmd_ty
         ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsCmd GhcTc -> HsCmd GhcTc
mkHsCmdWrap HsWrapper
wrap (XCmdLamCase GhcTc
-> LamCaseVariant -> MatchGroup GhcTc (LHsCmd GhcTc) -> HsCmd GhcTc
forall id.
XCmdLamCase id
-> LamCaseVariant -> MatchGroup id (LHsCmd id) -> HsCmd id
HsCmdLamCase XCmdLamCase GhcRn
XCmdLamCase GhcTc
x LamCaseVariant
lc_variant MatchGroup GhcTc (LHsCmd GhcTc)
MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
match')) }

tc_cmd CmdEnv
env (HsCmdIf XCmdIf GhcRn
x SyntaxExpr GhcRn
SyntaxExprRn
NoSyntaxExprRn LHsExpr GhcRn
pred LHsCmd GhcRn
b1 LHsCmd GhcRn
b2) (TcType, TcType)
res_ty    -- Ordinary 'if'
  = do  { GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred' <- LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
pred TcType
boolTy
        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
b1'   <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
b1 (TcType, TcType)
res_ty
        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
b2'   <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
b2 (TcType, TcType)
res_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdIf GhcTc
-> SyntaxExpr GhcTc
-> LHsExpr GhcTc
-> LHsCmd GhcTc
-> LHsCmd GhcTc
-> HsCmd GhcTc
forall id.
XCmdIf id
-> SyntaxExpr id
-> LHsExpr id
-> LHsCmd id
-> LHsCmd id
-> HsCmd id
HsCmdIf XCmdIf GhcRn
XCmdIf GhcTc
x SyntaxExpr GhcTc
SyntaxExprTc
NoSyntaxExprTc LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred' LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
b1' LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
b2')
    }

tc_cmd CmdEnv
env (HsCmdIf XCmdIf GhcRn
x fun :: SyntaxExpr GhcRn
fun@(SyntaxExprRn {}) LHsExpr GhcRn
pred LHsCmd GhcRn
b1 LHsCmd GhcRn
b2) (TcType, TcType)
res_ty -- Rebindable syntax for if
  = do  { TcType
pred_ty <- TcM TcType
newOpenFlexiTyVarTy
        -- For arrows, need ifThenElse :: forall r. T -> r -> r -> r
        -- because we're going to apply it to the environment, not
        -- the return value.
        ; SkolemInfo
skol_info <- SkolemInfoAnon -> IOEnv (Env TcGblEnv TcLclEnv) SkolemInfo
forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
mkSkolemInfo SkolemInfoAnon
ArrowReboundIfSkol
        ; (Subst
_, [TcTyVar
r_tv]) <- SkolemInfo -> [TcTyVar] -> TcM (Subst, [TcTyVar])
tcInstSkolTyVars SkolemInfo
skol_info [TcTyVar
alphaTyVar]
        ; let r_ty :: TcType
r_ty = TcTyVar -> TcType
mkTyVarTy TcTyVar
r_tv
        ; Bool -> TcRnMessage -> TcM ()
checkTc (Bool -> Bool
not (TcTyVar
r_tv TcTyVar -> VarSet -> Bool
`elemVarSet` TcType -> VarSet
tyCoVarsOfType TcType
pred_ty))
                  TcRnMessage
TcRnArrowIfThenElsePredDependsOnResultTy
        ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred', SyntaxExprTc
fun') <- CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpRhoType
-> ([TcType]
    -> [TcType]
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), SyntaxExprTc)
forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpRhoType
-> ([TcType] -> [TcType] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
IfThenElseOrigin SyntaxExpr GhcRn
SyntaxExprRn
fun
                              ((TcType -> SyntaxOpType) -> [TcType] -> [SyntaxOpType]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> SyntaxOpType
synKnownType [TcType
pred_ty, TcType
r_ty, TcType
r_ty])
                              (TcType -> ExpRhoType
mkCheckExpType TcType
r_ty) (([TcType]
  -> [TcType]
  -> IOEnv
       (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
 -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), SyntaxExprTc))
-> ([TcType]
    -> [TcType]
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc), SyntaxExprTc)
forall a b. (a -> b) -> a -> b
$ \ [TcType]
_ [TcType]
_ ->
                           LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
pred TcType
pred_ty

        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
b1'   <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
b1 (TcType, TcType)
res_ty
        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
b2'   <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
b2 (TcType, TcType)
res_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdIf GhcTc
-> SyntaxExpr GhcTc
-> LHsExpr GhcTc
-> LHsCmd GhcTc
-> LHsCmd GhcTc
-> HsCmd GhcTc
forall id.
XCmdIf id
-> SyntaxExpr id
-> LHsExpr id
-> LHsCmd id
-> LHsCmd id
-> HsCmd id
HsCmdIf XCmdIf GhcRn
XCmdIf GhcTc
x SyntaxExpr GhcTc
SyntaxExprTc
fun' LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred' LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
b1' LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
b2')
    }

-------------------------------------------
--              Arrow application
--          (f -< a)   or   (f -<< a)
--
--   D   |- fun :: a t1 t2
--   D,G |- arg :: t1
--  ------------------------
--   D;G |-a  fun -< arg :: stk --> t2
--
--   D,G |- fun :: a t1 t2
--   D,G |- arg :: t1
--  ------------------------
--   D;G |-a  fun -<< arg :: stk --> t2
--
-- (plus -<< requires ArrowApply)

tc_cmd CmdEnv
env cmd :: HsCmd GhcRn
cmd@(HsCmdArrApp XCmdArrApp GhcRn
_ LHsExpr GhcRn
fun LHsExpr GhcRn
arg HsArrAppType
ho_app Bool
lr) (TcType
_, TcType
res_ty)
  = SDoc -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
cmd)    (TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc))
-> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$
    do  { TcType
arg_ty <- TcM TcType
newOpenFlexiTyVarTy
        ; let fun_ty :: TcType
fun_ty = CmdEnv -> TcType -> TcType -> TcType
mkCmdArrTy CmdEnv
env TcType
arg_ty TcType
res_ty
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
fun' <- IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
select_arrow_scope (LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
fun TcType
fun_ty)

        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
arg' <- LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
arg TcType
arg_ty

        ; HasDebugCallStack => FixedRuntimeRepContext -> TcType -> TcM ()
FixedRuntimeRepContext -> TcType -> TcM ()
hasFixedRuntimeRep_syntactic
            (FRRArrowContext -> FixedRuntimeRepContext
FRRArrow (FRRArrowContext -> FixedRuntimeRepContext)
-> FRRArrowContext -> FixedRuntimeRepContext
forall a b. (a -> b) -> a -> b
$ HsExpr GhcRn -> HsExpr GhcRn -> HsArrAppType -> FRRArrowContext
ArrowCmdArrApp (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
fun) (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
arg) HsArrAppType
ho_app)
            TcType
fun_ty

        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdArrApp GhcTc
-> LHsExpr GhcTc
-> LHsExpr GhcTc
-> HsArrAppType
-> Bool
-> HsCmd GhcTc
forall id.
XCmdArrApp id
-> LHsExpr id -> LHsExpr id -> HsArrAppType -> Bool -> HsCmd id
HsCmdArrApp XCmdArrApp GhcTc
TcType
fun_ty LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
fun' LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
arg' HsArrAppType
ho_app Bool
lr) }
  where
       -- Before type-checking f, use the environment of the enclosing
       -- proc for the (-<) case.
       -- Local bindings, inside the enclosing proc, are not in scope
       -- inside f.  In the higher-order case (-<<), they are.
       -- See Note [Escaping the arrow scope] in GHC.Tc.Types
    select_arrow_scope :: IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
select_arrow_scope IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
tc = case HsArrAppType
ho_app of
        HsArrAppType
HsHigherOrderApp -> IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
tc
        HsArrAppType
HsFirstOrderApp  -> IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall a. TcM a -> TcM a
escapeArrowScope IOEnv
  (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsExpr GhcTc))
tc

-------------------------------------------
--              Command application
--
-- D,G |-  exp : t
-- D;G |-a cmd : (t,stk) --> res
-- -----------------------------
-- D;G |-a cmd exp : stk --> res

tc_cmd CmdEnv
env cmd :: HsCmd GhcRn
cmd@(HsCmdApp XCmdApp GhcRn
x LHsCmd GhcRn
fun LHsExpr GhcRn
arg) (TcType
cmd_stk, TcType
res_ty)
  = SDoc -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
cmd)    (TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc))
-> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a b. (a -> b) -> a -> b
$
    do  { TcType
arg_ty <- TcM TcType
newOpenFlexiTyVarTy
        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
fun'   <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
fun (TcType -> TcType -> TcType
mkPairTy TcType
arg_ty TcType
cmd_stk, TcType
res_ty)
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
arg'   <- LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
arg TcType
arg_ty
        ; HasDebugCallStack => FixedRuntimeRepContext -> TcType -> TcM ()
FixedRuntimeRepContext -> TcType -> TcM ()
hasFixedRuntimeRep_syntactic
            (FRRArrowContext -> FixedRuntimeRepContext
FRRArrow (FRRArrowContext -> FixedRuntimeRepContext)
-> FRRArrowContext -> FixedRuntimeRepContext
forall a b. (a -> b) -> a -> b
$ HsCmd GhcRn -> HsExpr GhcRn -> FRRArrowContext
ArrowCmdApp (GenLocated SrcSpanAnnA (HsCmd GhcRn) -> HsCmd GhcRn
forall l e. GenLocated l e -> e
unLoc LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
fun) (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
arg))
            TcType
arg_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdApp GhcTc -> LHsCmd GhcTc -> LHsExpr GhcTc -> HsCmd GhcTc
forall id. XCmdApp id -> LHsCmd id -> LHsExpr id -> HsCmd id
HsCmdApp XCmdApp GhcRn
XCmdApp GhcTc
x LHsCmd GhcTc
GenLocated SrcSpanAnnA (HsCmd GhcTc)
fun' LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
arg') }

-------------------------------------------
--              Lambda
--
-- D;G,x:t |-a cmd : stk --> res
-- ------------------------------
-- D;G |-a (\x.cmd) : (t,stk) --> res

tc_cmd CmdEnv
env (HsCmdLam XCmdLam GhcRn
x MatchGroup GhcRn (LHsCmd GhcRn)
match) (TcType, TcType)
cmd_ty
  = do { (HsWrapper
wrap, MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
match') <- CmdEnv
-> HsArrowMatchContext
-> MatchGroup GhcRn (LHsCmd GhcRn)
-> (TcType, TcType)
-> TcM (HsWrapper, MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatchLambda CmdEnv
env HsArrowMatchContext
KappaExpr MatchGroup GhcRn (LHsCmd GhcRn)
match (TcType, TcType)
cmd_ty
       ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsCmd GhcTc -> HsCmd GhcTc
mkHsCmdWrap HsWrapper
wrap (XCmdLam GhcTc -> MatchGroup GhcTc (LHsCmd GhcTc) -> HsCmd GhcTc
forall id. XCmdLam id -> MatchGroup id (LHsCmd id) -> HsCmd id
HsCmdLam XCmdLam GhcRn
XCmdLam GhcTc
x MatchGroup GhcTc (LHsCmd GhcTc)
MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
match')) }

-------------------------------------------
--              Do notation

tc_cmd CmdEnv
env (HsCmdDo XCmdDo GhcRn
_ (L SrcSpanAnnL
l [GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
stmts) ) (TcType
cmd_stk, TcType
res_ty)
  = do  { TcCoercion
co <- Maybe TypedThing -> TcType -> TcType -> TcM TcCoercion
unifyType Maybe TypedThing
forall a. Maybe a
Nothing TcType
unitTy TcType
cmd_stk  -- Expecting empty argument stack
        ; [GenLocated
   SrcSpanAnnA
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
stmts' <- HsStmtContext GhcTc
-> TcStmtChecker HsCmd TcType
-> [LStmt GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
-> TcType
-> TcM [LStmt GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))]
forall (body :: * -> *) rho_type.
AnnoBody body =>
HsStmtContext GhcTc
-> TcStmtChecker body rho_type
-> [LStmt GhcRn (LocatedA (body GhcRn))]
-> rho_type
-> TcM [LStmt GhcTc (LocatedA (body GhcTc))]
tcStmts HsStmtContext GhcTc
forall p. HsStmtContext p
ArrowExpr (CmdEnv -> TcStmtChecker HsCmd TcType
tcArrDoStmt CmdEnv
env) [LStmt GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
stmts TcType
res_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsCmd GhcTc -> HsCmd GhcTc
mkHsCmdWrap (TcCoercion -> HsWrapper
mkWpCastN TcCoercion
co) (XCmdDo GhcTc -> XRec GhcTc [CmdLStmt GhcTc] -> HsCmd GhcTc
forall id. XCmdDo id -> XRec id [CmdLStmt id] -> HsCmd id
HsCmdDo XCmdDo GhcTc
TcType
res_ty (SrcSpanAnnL
-> [GenLocated
      SrcSpanAnnA
      (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
-> GenLocated
     SrcSpanAnnL
     [GenLocated
        SrcSpanAnnA
        (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
l [GenLocated
   SrcSpanAnnA
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
stmts') )) }


-----------------------------------------------------------------
--      Arrow ``forms''       (| e c1 .. cn |)
--
--      D; G |-a1 c1 : stk1 --> r1
--      ...
--      D; G |-an cn : stkn --> rn
--      D |-  e :: forall e. a1 (e, stk1) t1
--                                ...
--                        -> an (e, stkn) tn
--                        -> a  (e, stk) t
--      e \not\in (stk, stk1, ..., stkm, t, t1, ..., tn)
--      ----------------------------------------------
--      D; G |-a  (| e c1 ... cn |)  :  stk --> t

tc_cmd CmdEnv
env cmd :: HsCmd GhcRn
cmd@(HsCmdArrForm XCmdArrForm GhcRn
x LHsExpr GhcRn
expr LexicalFixity
f Maybe Fixity
fixity [LHsCmdTop GhcRn]
cmd_args) (TcType
cmd_stk, TcType
res_ty)
  = SDoc -> TcM (HsCmd GhcTc) -> TcM (HsCmd GhcTc)
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
cmd)
    do  { ([GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)]
cmd_args', [TcType]
cmd_tys) <- (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcRn)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcType))
-> [GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcRn)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)], [TcType])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM LHsCmdTop GhcRn -> TcM (LHsCmdTop GhcTc, TcType)
GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcRn)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcType)
tc_cmd_arg [LHsCmdTop GhcRn]
[GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcRn)]
cmd_args
                              -- We use alphaTyVar for 'w'
        ; let e_ty :: TcType
e_ty = TcTyVar -> TcType -> TcType
mkInfForAllTy TcTyVar
alphaTyVar (TcType -> TcType) -> TcType -> TcType
forall a b. (a -> b) -> a -> b
$
                     [TcType] -> TcType -> TcType
mkVisFunTysMany [TcType]
cmd_tys (TcType -> TcType) -> TcType -> TcType
forall a b. (a -> b) -> a -> b
$
                     CmdEnv -> TcType -> TcType -> TcType
mkCmdArrTy CmdEnv
env (TcType -> TcType -> TcType
mkPairTy TcType
alphaTy TcType
cmd_stk) TcType
res_ty
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- LHsExpr GhcRn -> TcType -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr TcType
e_ty
        ; HsCmd GhcTc -> TcM (HsCmd GhcTc)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdArrForm GhcTc
-> LHsExpr GhcTc
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop GhcTc]
-> HsCmd GhcTc
forall id.
XCmdArrForm id
-> LHsExpr id
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop id]
-> HsCmd id
HsCmdArrForm XCmdArrForm GhcRn
XCmdArrForm GhcTc
x LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' LexicalFixity
f Maybe Fixity
fixity [LHsCmdTop GhcTc]
[GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)]
cmd_args') }

  where
    tc_cmd_arg :: LHsCmdTop GhcRn -> TcM (LHsCmdTop GhcTc, TcType)
    tc_cmd_arg :: LHsCmdTop GhcRn -> TcM (LHsCmdTop GhcTc, TcType)
tc_cmd_arg cmd :: LHsCmdTop GhcRn
cmd@(L SrcAnn NoEpAnns
loc (HsCmdTop XCmdTop GhcRn
names LHsCmd GhcRn
_))
       = do { TcType
arr_ty <- TcType -> TcM TcType
newFlexiTyVarTy TcType
arrowTyConKind
            ; TcType
stk_ty <- TcType -> TcM TcType
newFlexiTyVarTy TcType
liftedTypeKind
            ; TcType
res_ty <- TcType -> TcM TcType
newFlexiTyVarTy TcType
liftedTypeKind
            ; [(Name, HsExpr GhcTc)]
names' <- SrcAnn NoEpAnns
-> TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)]
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc (TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)])
-> TcRn [(Name, HsExpr GhcTc)] -> TcRn [(Name, HsExpr GhcTc)]
forall a b. (a -> b) -> a -> b
$
                ((Name, HsExpr GhcRn)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Name, HsExpr GhcTc))
-> [(Name, HsExpr GhcRn)] -> TcRn [(Name, HsExpr GhcTc)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (CtOrigin
-> TcType
-> (Name, HsExpr GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (Name, HsExpr GhcTc)
tcSyntaxName CtOrigin
ArrowCmdOrigin TcType
arr_ty) [(Name, HsExpr GhcRn)]
XCmdTop GhcRn
names
            ; let env' :: CmdEnv
env' = CmdEnv
env { cmd_arr = arr_ty }
            ; GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd' <- CmdEnv
-> [(Name, HsExpr GhcTc)]
-> LHsCmdTop GhcRn
-> (TcType, TcType)
-> TcM (LHsCmdTop GhcTc)
tcCmdTop CmdEnv
env' [(Name, HsExpr GhcTc)]
names' LHsCmdTop GhcRn
cmd (TcType
stk_ty, TcType
res_ty)
            ; (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc), TcType)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd',  CmdEnv -> TcType -> TcType -> TcType
mkCmdArrTy CmdEnv
env' (TcType -> TcType -> TcType
mkPairTy TcType
alphaTy TcType
stk_ty) TcType
res_ty) }

-- | Typechecking for case command alternatives. Used for 'HsCmdCase'.
tcCmdMatches :: CmdEnv
             -> TcTypeFRR -- ^ Type of the scrutinee.
             -> MatchGroup GhcRn (LHsCmd GhcRn)  -- ^ case alternatives
             -> CmdType
             -> TcM (MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatches :: CmdEnv
-> TcType
-> MatchGroup GhcRn (LHsCmd GhcRn)
-> (TcType, TcType)
-> TcM (MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatches CmdEnv
env TcType
scrut_ty MatchGroup GhcRn (LHsCmd GhcRn)
matches (TcType
stk, TcType
res_ty)
  = TcMatchCtxt HsCmd
-> Scaled TcType
-> MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> ExpRhoType
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall (body :: * -> *).
AnnoBody body =>
TcMatchCtxt body
-> Scaled TcType
-> MatchGroup GhcRn (LocatedA (body GhcRn))
-> ExpRhoType
-> TcM (MatchGroup GhcTc (LocatedA (body GhcTc)))
tcMatchesCase TcMatchCtxt HsCmd
match_ctxt (TcType -> Scaled TcType
forall a. a -> Scaled a
unrestricted TcType
scrut_ty) MatchGroup GhcRn (LHsCmd GhcRn)
MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
matches (TcType -> ExpRhoType
mkCheckExpType TcType
res_ty)
  where
    match_ctxt :: TcMatchCtxt HsCmd
match_ctxt = MC { mc_what :: HsMatchContext GhcTc
mc_what = HsArrowMatchContext -> HsMatchContext GhcTc
forall p. HsArrowMatchContext -> HsMatchContext p
ArrowMatchCtxt HsArrowMatchContext
ArrowCaseAlt,
                      mc_body :: GenLocated SrcSpanAnnA (HsCmd GhcRn)
-> ExpRhoType
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc))
mc_body = GenLocated SrcSpanAnnA (HsCmd GhcRn)
-> ExpRhoType
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc))
mc_body }
    mc_body :: GenLocated SrcSpanAnnA (HsCmd GhcRn)
-> ExpRhoType
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc))
mc_body GenLocated SrcSpanAnnA (HsCmd GhcRn)
body ExpRhoType
res_ty' = do { TcType
res_ty' <- ExpRhoType -> TcM TcType
expTypeToType ExpRhoType
res_ty'
                              ; CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
body (TcType
stk, TcType
res_ty') }

-- | Typechecking for 'HsCmdLam' and 'HsCmdLamCase'.
tcCmdMatchLambda :: CmdEnv
                 -> HsArrowMatchContext
                 -> MatchGroup GhcRn (LHsCmd GhcRn)
                 -> CmdType
                 -> TcM (HsWrapper, MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatchLambda :: CmdEnv
-> HsArrowMatchContext
-> MatchGroup GhcRn (LHsCmd GhcRn)
-> (TcType, TcType)
-> TcM (HsWrapper, MatchGroup GhcTc (LHsCmd GhcTc))
tcCmdMatchLambda CmdEnv
env
                 HsArrowMatchContext
ctxt
                 mg :: MatchGroup GhcRn (LHsCmd GhcRn)
mg@MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
l [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
matches, mg_ext :: forall p body. MatchGroup p body -> XMG p body
mg_ext = XMG GhcRn (LHsCmd GhcRn)
origin }
                 (TcType
cmd_stk, TcType
res_ty)
  = do { (TcCoercion
co, [TcType]
arg_tys, TcType
cmd_stk') <- Arity -> TcType -> TcM (TcCoercion, [TcType], TcType)
matchExpectedCmdArgs Arity
n_pats TcType
cmd_stk

       ; let check_arg_tys :: [Scaled ExpRhoType]
check_arg_tys = (TcType -> Scaled ExpRhoType) -> [TcType] -> [Scaled ExpRhoType]
forall a b. (a -> b) -> [a] -> [b]
map (ExpRhoType -> Scaled ExpRhoType
forall a. a -> Scaled a
unrestricted (ExpRhoType -> Scaled ExpRhoType)
-> (TcType -> ExpRhoType) -> TcType -> Scaled ExpRhoType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcType -> ExpRhoType
mkCheckExpType) [TcType]
arg_tys
       ; [GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
matches' <- [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> IOEnv
         (Env TcGblEnv TcLclEnv)
         (GenLocated
            SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
matches ((GenLocated
    SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
  -> IOEnv
       (Env TcGblEnv TcLclEnv)
       (GenLocated
          SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      [GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))])
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> IOEnv
         (Env TcGblEnv TcLclEnv)
         (GenLocated
            SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
forall a b. (a -> b) -> a -> b
$
           SDoc
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (SDoc
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> SDoc)
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)) -> SDoc
forall (idR :: Pass) body.
(OutputableBndrId idR, Outputable body) =>
Match (GhcPass idR) body -> SDoc
pprMatchInCtxt (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)) -> SDoc)
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated
  SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
forall l e. GenLocated l e -> e
unLoc (GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> IOEnv
         (Env TcGblEnv TcLclEnv)
         (GenLocated
            SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall a b.
(GenLocated
   SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
 -> a -> b)
-> (GenLocated
      SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
    -> a)
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Scaled ExpRhoType]
-> TcType
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
tc_match [Scaled ExpRhoType]
check_arg_tys TcType
cmd_stk'

       ; let arg_tys' :: [Scaled TcType]
arg_tys' = (TcType -> Scaled TcType) -> [TcType] -> [Scaled TcType]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> Scaled TcType
forall a. a -> Scaled a
unrestricted [TcType]
arg_tys
             mg' :: MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
mg' = MatchGroup GhcRn (LHsCmd GhcRn)
mg { mg_alts = L l matches'
                      , mg_ext = MatchGroupTc arg_tys' res_ty origin }

       ; (HsWrapper,
 MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsWrapper,
      MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercion -> HsWrapper
mkWpCastN TcCoercion
co, MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
mg') }
  where
    n_pats :: Arity
n_pats | MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)) -> Bool
forall (p :: Pass) body. MatchGroup (GhcPass p) body -> Bool
isEmptyMatchGroup MatchGroup GhcRn (LHsCmd GhcRn)
MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
mg = Arity
1   -- must be lambda-case
           | Bool
otherwise            = MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)) -> Arity
forall (id :: Pass) body. MatchGroup (GhcPass id) body -> Arity
matchGroupArity MatchGroup GhcRn (LHsCmd GhcRn)
MatchGroup GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
mg

    -- Check the patterns, and the GRHSs inside
    tc_match :: [Scaled ExpRhoType]
-> TcType
-> GenLocated
     SrcSpanAnnA (Match GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
tc_match [Scaled ExpRhoType]
arg_tys TcType
cmd_stk' (L SrcSpanAnnA
mtch_loc (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcRn]
pats, m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
grhss }))
      = do { ([GenLocated SrcSpanAnnA (Pat GhcTc)]
pats', GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
grhss') <- SrcSpanAnnA
-> TcRn
     ([GenLocated SrcSpanAnnA (Pat GhcTc)],
      GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcRn
     ([GenLocated SrcSpanAnnA (Pat GhcTc)],
      GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
mtch_loc           (TcRn
   ([GenLocated SrcSpanAnnA (Pat GhcTc)],
    GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
 -> TcRn
      ([GenLocated SrcSpanAnnA (Pat GhcTc)],
       GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
-> TcRn
     ([GenLocated SrcSpanAnnA (Pat GhcTc)],
      GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcRn
     ([GenLocated SrcSpanAnnA (Pat GhcTc)],
      GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a b. (a -> b) -> a -> b
$
                                HsMatchContext GhcTc
-> [LPat GhcRn]
-> [Scaled ExpRhoType]
-> TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcM
     ([LPat GhcTc], GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a.
HsMatchContext GhcTc
-> [LPat GhcRn]
-> [Scaled ExpRhoType]
-> TcM a
-> TcM ([LPat GhcTc], a)
tcPats HsMatchContext GhcTc
match_ctxt [LPat GhcRn]
pats [Scaled ExpRhoType]
arg_tys (TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
 -> TcM
      ([LPat GhcTc], GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
-> TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcM
     ([LPat GhcTc], GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a b. (a -> b) -> a -> b
$
                                GRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> TcType
-> ExpRhoType
-> TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
tc_grhss GRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
grhss TcType
cmd_stk' (TcType -> ExpRhoType
mkCheckExpType TcType
res_ty)

           ; GenLocated
  SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated
   SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> GenLocated
     SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall a b. (a -> b) -> a -> b
$ SrcSpanAnnA
-> Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> GenLocated
     SrcSpanAnnA (Match GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
mtch_loc (Match { m_ext :: XCMatch GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
m_ext = XCMatch GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn
                                        , m_ctxt :: HsMatchContext GhcTc
m_ctxt = HsMatchContext GhcTc
match_ctxt
                                        , m_pats :: [LPat GhcTc]
m_pats = [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats'
                                        , m_grhss :: GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
m_grhss = GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
grhss' }) }

    match_ctxt :: HsMatchContext GhcTc
match_ctxt = HsArrowMatchContext -> HsMatchContext GhcTc
forall p. HsArrowMatchContext -> HsMatchContext p
ArrowMatchCtxt HsArrowMatchContext
ctxt
    pg_ctxt :: HsStmtContext GhcTc
pg_ctxt    = HsMatchContext GhcTc -> HsStmtContext GhcTc
forall p. HsMatchContext p -> HsStmtContext p
PatGuard HsMatchContext GhcTc
match_ctxt

    tc_grhss :: GRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> TcType
-> ExpRhoType
-> TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
tc_grhss (GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
x [LGRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
grhss HsLocalBinds GhcRn
binds) TcType
stk_ty ExpRhoType
res_ty
        = do { (HsLocalBinds GhcTc
binds', [GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
grhss') <- HsLocalBinds GhcRn
-> TcM
     [GenLocated
        (SrcAnn NoEpAnns)
        (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
-> TcM
     (HsLocalBinds GhcTc,
      [GenLocated
         (SrcAnn NoEpAnns)
         (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))])
forall thing.
HsLocalBinds GhcRn -> TcM thing -> TcM (HsLocalBinds GhcTc, thing)
tcLocalBinds HsLocalBinds GhcRn
binds (TcM
   [GenLocated
      (SrcAnn NoEpAnns)
      (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
 -> TcM
      (HsLocalBinds GhcTc,
       [GenLocated
          (SrcAnn NoEpAnns)
          (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]))
-> TcM
     [GenLocated
        (SrcAnn NoEpAnns)
        (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
-> TcM
     (HsLocalBinds GhcTc,
      [GenLocated
         (SrcAnn NoEpAnns)
         (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))])
forall a b. (a -> b) -> a -> b
$
                                   (GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         (SrcAnn NoEpAnns)
         (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))))
-> [GenLocated
      (SrcAnn NoEpAnns)
      (GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
-> TcM
     [GenLocated
        (SrcAnn NoEpAnns)
        (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
 -> TcM (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
-> GenLocated
     (SrcAnn NoEpAnns)
     (GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        (SrcAnn NoEpAnns)
        (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))))
forall a b ann.
(a -> TcM b)
-> GenLocated (SrcSpanAnn' ann) a
-> TcRn (GenLocated (SrcSpanAnn' ann) b)
wrapLocMA (TcType
-> ExpRhoType
-> GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> TcM (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
tc_grhs TcType
stk_ty ExpRhoType
res_ty)) [LGRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
[GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
grhss
             ; GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> TcM (GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCGRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> [LGRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))]
-> HsLocalBinds GhcTc
-> GRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs XCGRHSs GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
XCGRHSs GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
x [LGRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))]
[GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
grhss' HsLocalBinds GhcTc
binds') }

    tc_grhs :: TcType
-> ExpRhoType
-> GRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
-> TcM (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
tc_grhs TcType
stk_ty ExpRhoType
res_ty (GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
x [GuardLStmt GhcRn]
guards GenLocated SrcSpanAnnA (HsCmd GhcRn)
body)
        = do { ([GenLocated
   SrcSpanAnnA
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
guards', GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs') <- HsStmtContext GhcTc
-> TcStmtChecker HsExpr ExpRhoType
-> [LStmt GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
-> ExpRhoType
-> (ExpRhoType
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcM
     ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))],
      GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall (body :: * -> *) rho_type thing.
AnnoBody body =>
HsStmtContext GhcTc
-> TcStmtChecker body rho_type
-> [LStmt GhcRn (LocatedA (body GhcRn))]
-> rho_type
-> (rho_type -> TcM thing)
-> TcM ([LStmt GhcTc (LocatedA (body GhcTc))], thing)
tcStmtsAndThen HsStmtContext GhcTc
pg_ctxt HsStmtContext GhcTc
-> Stmt GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))
-> ExpRhoType
-> (ExpRhoType -> TcM thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)), thing)
TcStmtChecker HsExpr ExpRhoType
tcGuardStmt [GuardLStmt GhcRn]
[LStmt GhcRn (GenLocated SrcSpanAnnA (HsExpr GhcRn))]
guards ExpRhoType
res_ty ((ExpRhoType
  -> IOEnv
       (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
 -> TcM
      ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))],
       GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> (ExpRhoType
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
-> TcM
     ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))],
      GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall a b. (a -> b) -> a -> b
$
                                  \ ExpRhoType
res_ty -> CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
body
                                                (TcType
stk_ty, String -> ExpRhoType -> TcType
checkingExpType String
"tc_grhs" ExpRhoType
res_ty)
             ; GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> TcM (GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCGRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> [GuardLStmt GhcTc]
-> GenLocated SrcSpanAnnA (HsCmd GhcTc)
-> GRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS XCGRHS GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
XCGRHS GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
x [GuardLStmt GhcTc]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
guards' GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs') }

matchExpectedCmdArgs :: Arity -> TcType -> TcM (TcCoercionN, [TcTypeFRR], TcType)
matchExpectedCmdArgs :: Arity -> TcType -> TcM (TcCoercion, [TcType], TcType)
matchExpectedCmdArgs Arity
0 TcType
ty
  = (TcCoercion, [TcType], TcType)
-> TcM (TcCoercion, [TcType], TcType)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (TcType -> TcCoercion
mkNomReflCo TcType
ty, [], TcType
ty)
matchExpectedCmdArgs Arity
n TcType
ty
  = do { (TcCoercion
co1, [TcType
ty1, TcType
ty2]) <- TyCon -> TcType -> TcM (TcCoercion, [TcType])
matchExpectedTyConApp TyCon
pairTyCon TcType
ty
       ; (TcCoercion
co2, [TcType]
tys, TcType
res_ty) <- Arity -> TcType -> TcM (TcCoercion, [TcType], TcType)
matchExpectedCmdArgs (Arity
nArity -> Arity -> Arity
forall a. Num a => a -> a -> a
-Arity
1) TcType
ty2
       ; (TcCoercion, [TcType], TcType)
-> TcM (TcCoercion, [TcType], TcType)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HasDebugCallStack => Role -> TyCon -> [TcCoercion] -> TcCoercion
Role -> TyCon -> [TcCoercion] -> TcCoercion
mkTyConAppCo Role
Nominal TyCon
pairTyCon [TcCoercion
co1, TcCoercion
co2], TcType
ty1TcType -> [TcType] -> [TcType]
forall a. a -> [a] -> [a]
:[TcType]
tys, TcType
res_ty) }

{-
************************************************************************
*                                                                      *
                Stmts
*                                                                      *
************************************************************************
-}

--------------------------------
--      Mdo-notation
-- The distinctive features here are
--      (a) RecStmts, and
--      (b) no rebindable syntax

tcArrDoStmt :: CmdEnv -> TcCmdStmtChecker
tcArrDoStmt :: CmdEnv -> TcStmtChecker HsCmd TcType
tcArrDoStmt CmdEnv
env HsStmtContext GhcTc
_ (LastStmt XLastStmt GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
x GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs Maybe Bool
noret SyntaxExpr GhcRn
_) TcType
res_ty TcType -> TcM thing
thing_inside
  = do  { GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs' <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs (TcType
unitTy, TcType
res_ty)
        ; thing
thing <- TcType -> TcM thing
thing_inside (String -> TcType
forall a. HasCallStack => String -> a
panic String
"tcArrDoStmt")
        ; (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XLastStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> GenLocated SrcSpanAnnA (HsCmd GhcTc)
-> Maybe Bool
-> SyntaxExpr GhcTc
-> StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall idL idR body.
XLastStmt idL idR body
-> body -> Maybe Bool -> SyntaxExpr idR -> StmtLR idL idR body
LastStmt XLastStmt GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
XLastStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
x GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs' Maybe Bool
noret SyntaxExpr GhcTc
forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr, thing
thing) }

tcArrDoStmt CmdEnv
env HsStmtContext GhcTc
_ (BodyStmt XBodyStmt GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
_ GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs SyntaxExpr GhcRn
_ SyntaxExpr GhcRn
_) TcType
res_ty TcType -> TcM thing
thing_inside
  = do  { (GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs', TcType
elt_ty) <- CmdEnv -> LHsCmd GhcRn -> TcM (LHsCmd GhcTc, TcType)
tc_arr_rhs CmdEnv
env LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs
        ; thing
thing          <- TcType -> TcM thing
thing_inside TcType
res_ty
        ; (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XBodyStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
-> GenLocated SrcSpanAnnA (HsCmd GhcTc)
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall idL idR body.
XBodyStmt idL idR body
-> body -> SyntaxExpr idR -> SyntaxExpr idR -> StmtLR idL idR body
BodyStmt XBodyStmt GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
TcType
elt_ty GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs' SyntaxExpr GhcTc
forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr SyntaxExpr GhcTc
forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr, thing
thing) }

tcArrDoStmt CmdEnv
env HsStmtContext GhcTc
ctxt (BindStmt XBindStmt GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
_ LPat GhcRn
pat GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs) TcType
res_ty TcType -> TcM thing
thing_inside
  = do  { (GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs', TcType
pat_ty) <- CmdEnv -> LHsCmd GhcRn -> TcM (LHsCmd GhcTc, TcType)
tc_arr_rhs CmdEnv
env LHsCmd GhcRn
GenLocated SrcSpanAnnA (HsCmd GhcRn)
rhs
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', thing
thing)  <- HsMatchContext GhcTc
-> LPat GhcRn
-> Scaled TcType
-> TcM thing
-> TcM (LPat GhcTc, thing)
forall a.
HsMatchContext GhcTc
-> LPat GhcRn -> Scaled TcType -> TcM a -> TcM (LPat GhcTc, a)
tcCheckPat (HsStmtContext GhcTc -> HsMatchContext GhcTc
forall p. HsStmtContext p -> HsMatchContext p
StmtCtxt HsStmtContext GhcTc
ctxt) LPat GhcRn
pat (TcType -> Scaled TcType
forall a. a -> Scaled a
unrestricted TcType
pat_ty) (TcM thing -> TcM (LPat GhcTc, thing))
-> TcM thing -> TcM (LPat GhcTc, thing)
forall a b. (a -> b) -> a -> b
$
                            TcType -> TcM thing
thing_inside TcType
res_ty
        ; (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LPat GhcTc
-> GenLocated SrcSpanAnnA (HsCmd GhcTc)
-> StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
forall (bodyR :: * -> *).
LPat GhcTc
-> LocatedA (bodyR GhcTc)
-> StmtLR GhcTc GhcTc (LocatedA (bodyR GhcTc))
mkTcBindStmt LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat' GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs', thing
thing) }

tcArrDoStmt CmdEnv
env HsStmtContext GhcTc
ctxt (RecStmt { recS_stmts :: forall idL idR body.
StmtLR idL idR body -> XRec idR [LStmtLR idL idR body]
recS_stmts = L SrcSpanAnnL
l [GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
stmts, recS_later_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_later_ids = [IdP GhcRn]
later_names
                            , recS_rec_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_rec_ids = [IdP GhcRn]
rec_names }) TcType
res_ty TcType -> TcM thing
thing_inside
  = do  { let tup_names :: [Name]
tup_names = [IdP GhcRn]
[Name]
rec_names [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ (Name -> Bool) -> [Name] -> [Name]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (IdP GhcRn -> [IdP GhcRn] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [IdP GhcRn]
rec_names) [IdP GhcRn]
[Name]
later_names
        ; [TcType]
tup_elt_tys <- Arity -> TcType -> TcM [TcType]
newFlexiTyVarTys ([Name] -> Arity
forall a. [a] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Name]
tup_names) TcType
liftedTypeKind
        ; let tup_ids :: [TcTyVar]
tup_ids = (Name -> TcType -> TcTyVar) -> [Name] -> [TcType] -> [TcTyVar]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Name
n TcType
p -> HasDebugCallStack => Name -> TcType -> TcType -> TcTyVar
Name -> TcType -> TcType -> TcTyVar
mkLocalId Name
n TcType
ManyTy TcType
p) [Name]
tup_names [TcType]
tup_elt_tys
                        -- Many because it's a recursive definition
        ; [TcTyVar]
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. [TcTyVar] -> TcM a -> TcM a
tcExtendIdEnv [TcTyVar]
tup_ids (TcM
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
 -> TcM
      (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing))
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a b. (a -> b) -> a -> b
$ do
        { ([GenLocated
   SrcSpanAnnA
   (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)))]
stmts', [HsExpr GhcTc]
tup_rets)
                <- HsStmtContext GhcTc
-> TcStmtChecker HsCmd TcType
-> [LStmt GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
-> TcType
-> (TcType -> TcM [HsExpr GhcTc])
-> TcM
     ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))],
      [HsExpr GhcTc])
forall (body :: * -> *) rho_type thing.
AnnoBody body =>
HsStmtContext GhcTc
-> TcStmtChecker body rho_type
-> [LStmt GhcRn (LocatedA (body GhcRn))]
-> rho_type
-> (rho_type -> TcM thing)
-> TcM ([LStmt GhcTc (LocatedA (body GhcTc))], thing)
tcStmtsAndThen HsStmtContext GhcTc
ctxt (CmdEnv -> TcStmtChecker HsCmd TcType
tcArrDoStmt CmdEnv
env) [LStmt GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))]
[GenLocated
   SrcSpanAnnA
   (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)))]
stmts TcType
res_ty   ((TcType -> TcM [HsExpr GhcTc])
 -> TcM
      ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))],
       [HsExpr GhcTc]))
-> (TcType -> TcM [HsExpr GhcTc])
-> TcM
     ([LStmt GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))],
      [HsExpr GhcTc])
forall a b. (a -> b) -> a -> b
$ \ TcType
_res_ty' ->
                        -- ToDo: res_ty not really right
                   (Name
 -> ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) (HsExpr GhcTc))
-> [Name] -> [ExpRhoType] -> TcM [HsExpr GhcTc]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Name -> ExpRhoType -> IOEnv (Env TcGblEnv TcLclEnv) (HsExpr GhcTc)
tcCheckId [Name]
tup_names ((TcType -> ExpRhoType) -> [TcType] -> [ExpRhoType]
forall a b. (a -> b) -> [a] -> [b]
map TcType -> ExpRhoType
mkCheckExpType [TcType]
tup_elt_tys)

        ; thing
thing <- TcType -> TcM thing
thing_inside TcType
res_ty
                -- NB:  The rec_ids for the recursive things
                --      already scope over this part. This binding may shadow
                --      some of them with polymorphic things with the same Name
                --      (see Note [How RecStmt works] in Language.Haskell.Syntax.Expr)

        ; let rec_ids :: [TcTyVar]
rec_ids = [Name] -> [TcTyVar] -> [TcTyVar]
forall b a. [b] -> [a] -> [a]
takeList [IdP GhcRn]
[Name]
rec_names [TcTyVar]
tup_ids
        ; [TcTyVar]
later_ids <- [Name] -> TcM [TcTyVar]
tcLookupLocalIds [IdP GhcRn]
[Name]
later_names

        ; let rec_rets :: [HsExpr GhcTc]
rec_rets = [Name] -> [HsExpr GhcTc] -> [HsExpr GhcTc]
forall b a. [b] -> [a] -> [a]
takeList [IdP GhcRn]
[Name]
rec_names [HsExpr GhcTc]
tup_rets
        ; let ret_table :: [(TcTyVar, HsExpr GhcTc)]
ret_table = [TcTyVar] -> [HsExpr GhcTc] -> [(TcTyVar, HsExpr GhcTc)]
forall a b. [a] -> [b] -> [(a, b)]
zip [TcTyVar]
tup_ids [HsExpr GhcTc]
tup_rets
        ; let later_rets :: [HsExpr GhcTc]
later_rets = [HsExpr GhcTc
r | TcTyVar
i <- [TcTyVar]
later_ids, (TcTyVar
j, HsExpr GhcTc
r) <- [(TcTyVar, HsExpr GhcTc)]
ret_table, TcTyVar
i TcTyVar -> TcTyVar -> Bool
forall a. Eq a => a -> a -> Bool
== TcTyVar
j]

        ; let
            stmt :: Stmt GhcTc (LocatedA (HsCmd GhcTc))
            stmt :: StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
stmt = StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
emptyRecStmtId
                                 { recS_stmts = L l stmts'
                                 -- { recS_stmts = _ stmts'
                                 , recS_later_ids = later_ids
                                 , recS_rec_ids = rec_ids
                                 , recS_ext = unitRecStmtTc
                                     { recS_later_rets = later_rets
                                     , recS_rec_rets = rec_rets
                                     , recS_ret_ty = res_ty} }
        ; (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc))
stmt, thing
thing)
        }}

tcArrDoStmt CmdEnv
_ HsStmtContext GhcTc
_ StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
stmt TcType
_ TcType -> TcM thing
_
  = String
-> SDoc
-> TcM
     (StmtLR GhcTc GhcTc (GenLocated SrcSpanAnnA (HsCmd GhcTc)), thing)
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcArrDoStmt: unexpected Stmt" (StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr StmtLR GhcRn GhcRn (GenLocated SrcSpanAnnA (HsCmd GhcRn))
stmt)

tc_arr_rhs :: CmdEnv -> LHsCmd GhcRn -> TcM (LHsCmd GhcTc, TcType)
tc_arr_rhs :: CmdEnv -> LHsCmd GhcRn -> TcM (LHsCmd GhcTc, TcType)
tc_arr_rhs CmdEnv
env LHsCmd GhcRn
rhs = do { TcType
ty <- TcType -> TcM TcType
newFlexiTyVarTy TcType
liftedTypeKind
                        ; GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs' <- CmdEnv -> LHsCmd GhcRn -> (TcType, TcType) -> TcM (LHsCmd GhcTc)
tcCmd CmdEnv
env LHsCmd GhcRn
rhs (TcType
unitTy, TcType
ty)
                        ; (GenLocated SrcSpanAnnA (HsCmd GhcTc), TcType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpanAnnA (HsCmd GhcTc), TcType)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpanAnnA (HsCmd GhcTc)
rhs', TcType
ty) }

{-
************************************************************************
*                                                                      *
                Helpers
*                                                                      *
************************************************************************
-}

mkPairTy :: Type -> Type -> Type
mkPairTy :: TcType -> TcType -> TcType
mkPairTy TcType
t1 TcType
t2 = TyCon -> [TcType] -> TcType
mkTyConApp TyCon
pairTyCon [TcType
t1,TcType
t2]

arrowTyConKind :: Kind          --  *->*->*
arrowTyConKind :: TcType
arrowTyConKind = [TcType] -> TcType -> TcType
mkVisFunTysMany [TcType
liftedTypeKind, TcType
liftedTypeKind] TcType
liftedTypeKind

{-
************************************************************************
*                                                                      *
                Errors
*                                                                      *
************************************************************************
-}

cmdCtxt :: HsCmd GhcRn -> SDoc
cmdCtxt :: HsCmd GhcRn -> SDoc
cmdCtxt HsCmd GhcRn
cmd = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the command:" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> HsCmd GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsCmd GhcRn
cmd