{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Type checker building blocks that do not involve unification.
module Language.Futhark.TypeChecker.Types
  ( checkTypeExp,
    checkTypeDecl,
    unifyTypesU,
    subtypeOf,
    subuniqueOf,
    checkForDuplicateNames,
    checkTypeParams,
    typeParamToArg,
    TypeSub (..),
    TypeSubs,
    substituteTypes,
    Subst (..),
    Substitutable (..),
    substTypesAny,
  )
where

import Control.Monad.Identity
import Control.Monad.Reader
import Control.Monad.State
import Data.Bifunctor
import Data.List (foldl', sort)
import qualified Data.Map.Strict as M
import Data.Maybe
import Futhark.Util (nubOrd)
import Futhark.Util.Pretty
import Language.Futhark
import Language.Futhark.Traversals
import Language.Futhark.TypeChecker.Monad

-- | @unifyTypes uf t1 t2@ attempts to unify @t1@ and @t2@.  If
-- unification cannot happen, 'Nothing' is returned, otherwise a type
-- that combines the aliasing of @t1@ and @t2@ is returned.
-- Uniqueness is unified with @uf@.
unifyTypesU ::
  (Monoid als, ArrayDim dim) =>
  (Uniqueness -> Uniqueness -> Maybe Uniqueness) ->
  TypeBase dim als ->
  TypeBase dim als ->
  Maybe (TypeBase dim als)
unifyTypesU :: forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (Array als
als1 Uniqueness
u1 ScalarTypeBase dim ()
et1 ShapeDecl dim
shape1) (Array als
als2 Uniqueness
u2 ScalarTypeBase dim ()
et2 ShapeDecl dim
shape2) =
  als
-> Uniqueness
-> ScalarTypeBase dim ()
-> ShapeDecl dim
-> TypeBase dim als
forall dim as.
as
-> Uniqueness
-> ScalarTypeBase dim ()
-> ShapeDecl dim
-> TypeBase dim as
Array (als
als1 als -> als -> als
forall a. Semigroup a => a -> a -> a
<> als
als2) (Uniqueness
 -> ScalarTypeBase dim () -> ShapeDecl dim -> TypeBase dim als)
-> Maybe Uniqueness
-> Maybe
     (ScalarTypeBase dim () -> ShapeDecl dim -> TypeBase dim als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Uniqueness -> Uniqueness -> Maybe Uniqueness
uf Uniqueness
u1 Uniqueness
u2
    Maybe (ScalarTypeBase dim () -> ShapeDecl dim -> TypeBase dim als)
-> Maybe (ScalarTypeBase dim ())
-> Maybe (ShapeDecl dim -> TypeBase dim als)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> ScalarTypeBase dim ()
-> ScalarTypeBase dim ()
-> Maybe (ScalarTypeBase dim ())
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> ScalarTypeBase dim als
-> ScalarTypeBase dim als
-> Maybe (ScalarTypeBase dim als)
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf ScalarTypeBase dim ()
et1 ScalarTypeBase dim ()
et2
    Maybe (ShapeDecl dim -> TypeBase dim als)
-> Maybe (ShapeDecl dim) -> Maybe (TypeBase dim als)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ShapeDecl dim -> ShapeDecl dim -> Maybe (ShapeDecl dim)
forall dim.
ArrayDim dim =>
ShapeDecl dim -> ShapeDecl dim -> Maybe (ShapeDecl dim)
unifyShapes ShapeDecl dim
shape1 ShapeDecl dim
shape2
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (Scalar ScalarTypeBase dim als
t1) (Scalar ScalarTypeBase dim als
t2) = ScalarTypeBase dim als -> TypeBase dim als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase dim als -> TypeBase dim als)
-> Maybe (ScalarTypeBase dim als) -> Maybe (TypeBase dim als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> ScalarTypeBase dim als
-> ScalarTypeBase dim als
-> Maybe (ScalarTypeBase dim als)
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> ScalarTypeBase dim als
-> ScalarTypeBase dim als
-> Maybe (ScalarTypeBase dim als)
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf ScalarTypeBase dim als
t1 ScalarTypeBase dim als
t2
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
_ TypeBase dim als
_ TypeBase dim als
_ = Maybe (TypeBase dim als)
forall a. Maybe a
Nothing

unifyScalarTypes ::
  (Monoid als, ArrayDim dim) =>
  (Uniqueness -> Uniqueness -> Maybe Uniqueness) ->
  ScalarTypeBase dim als ->
  ScalarTypeBase dim als ->
  Maybe (ScalarTypeBase dim als)
unifyScalarTypes :: forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> ScalarTypeBase dim als
-> ScalarTypeBase dim als
-> Maybe (ScalarTypeBase dim als)
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
_ (Prim PrimType
t1) (Prim PrimType
t2)
  | PrimType
t1 PrimType -> PrimType -> Bool
forall a. Eq a => a -> a -> Bool
== PrimType
t2 = ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als)
forall a. a -> Maybe a
Just (ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als))
-> ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als)
forall a b. (a -> b) -> a -> b
$ PrimType -> ScalarTypeBase dim als
forall dim as. PrimType -> ScalarTypeBase dim as
Prim PrimType
t1
  | Bool
otherwise = Maybe (ScalarTypeBase dim als)
forall a. Maybe a
Nothing
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (TypeVar als
als1 Uniqueness
u1 TypeName
t1 [TypeArg dim]
targs1) (TypeVar als
als2 Uniqueness
u2 TypeName
t2 [TypeArg dim]
targs2)
  | TypeName
t1 TypeName -> TypeName -> Bool
forall a. Eq a => a -> a -> Bool
== TypeName
t2 = do
    Uniqueness
u3 <- Uniqueness -> Uniqueness -> Maybe Uniqueness
uf Uniqueness
u1 Uniqueness
u2
    [TypeArg dim]
targs3 <- (TypeArg dim -> TypeArg dim -> Maybe (TypeArg dim))
-> [TypeArg dim] -> [TypeArg dim] -> Maybe [TypeArg dim]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM ((Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeArg dim -> TypeArg dim -> Maybe (TypeArg dim)
forall dim.
ArrayDim dim =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeArg dim -> TypeArg dim -> Maybe (TypeArg dim)
unifyTypeArgs Uniqueness -> Uniqueness -> Maybe Uniqueness
uf) [TypeArg dim]
targs1 [TypeArg dim]
targs2
    ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als)
forall a. a -> Maybe a
Just (ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als))
-> ScalarTypeBase dim als -> Maybe (ScalarTypeBase dim als)
forall a b. (a -> b) -> a -> b
$ als
-> Uniqueness
-> TypeName
-> [TypeArg dim]
-> ScalarTypeBase dim als
forall dim as.
as
-> Uniqueness -> TypeName -> [TypeArg dim] -> ScalarTypeBase dim as
TypeVar (als
als1 als -> als -> als
forall a. Semigroup a => a -> a -> a
<> als
als2) Uniqueness
u3 TypeName
t1 [TypeArg dim]
targs3
  | Bool
otherwise = Maybe (ScalarTypeBase dim als)
forall a. Maybe a
Nothing
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (Record Map Name (TypeBase dim als)
ts1) (Record Map Name (TypeBase dim als)
ts2)
  | Map Name (TypeBase dim als) -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Map Name (TypeBase dim als)
ts1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Map Name (TypeBase dim als) -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Map Name (TypeBase dim als)
ts2,
    [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort (Map Name (TypeBase dim als) -> [Name]
forall k a. Map k a -> [k]
M.keys Map Name (TypeBase dim als)
ts1) [Name] -> [Name] -> Bool
forall a. Eq a => a -> a -> Bool
== [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort (Map Name (TypeBase dim als) -> [Name]
forall k a. Map k a -> [k]
M.keys Map Name (TypeBase dim als)
ts2) =
    Map Name (TypeBase dim als) -> ScalarTypeBase dim als
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
Record
      (Map Name (TypeBase dim als) -> ScalarTypeBase dim als)
-> Maybe (Map Name (TypeBase dim als))
-> Maybe (ScalarTypeBase dim als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((TypeBase dim als, TypeBase dim als) -> Maybe (TypeBase dim als))
-> Map Name (TypeBase dim als, TypeBase dim als)
-> Maybe (Map Name (TypeBase dim als))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
        ((TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als))
-> (TypeBase dim als, TypeBase dim als) -> Maybe (TypeBase dim als)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf))
        ((TypeBase dim als
 -> TypeBase dim als -> (TypeBase dim als, TypeBase dim als))
-> Map Name (TypeBase dim als)
-> Map Name (TypeBase dim als)
-> Map Name (TypeBase dim als, TypeBase dim als)
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
M.intersectionWith (,) Map Name (TypeBase dim als)
ts1 Map Name (TypeBase dim als)
ts2)
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (Arrow als
as1 PName
mn1 TypeBase dim als
t1 TypeBase dim als
t1') (Arrow als
as2 PName
_ TypeBase dim als
t2 TypeBase dim als
t2') =
  als
-> PName
-> TypeBase dim als
-> TypeBase dim als
-> ScalarTypeBase dim als
forall dim as.
as
-> PName
-> TypeBase dim as
-> TypeBase dim as
-> ScalarTypeBase dim as
Arrow (als
as1 als -> als -> als
forall a. Semigroup a => a -> a -> a
<> als
as2) PName
mn1 (TypeBase dim als -> TypeBase dim als -> ScalarTypeBase dim als)
-> Maybe (TypeBase dim als)
-> Maybe (TypeBase dim als -> ScalarTypeBase dim als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU ((Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> Uniqueness -> Uniqueness -> Maybe Uniqueness
forall a b c. (a -> b -> c) -> b -> a -> c
flip Uniqueness -> Uniqueness -> Maybe Uniqueness
uf) TypeBase dim als
t1 TypeBase dim als
t2 Maybe (TypeBase dim als -> ScalarTypeBase dim als)
-> Maybe (TypeBase dim als) -> Maybe (ScalarTypeBase dim als)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf TypeBase dim als
t1' TypeBase dim als
t2'
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (Sum Map Name [TypeBase dim als]
cs1) (Sum Map Name [TypeBase dim als]
cs2)
  | Map Name [TypeBase dim als] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Map Name [TypeBase dim als]
cs1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Map Name [TypeBase dim als] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Map Name [TypeBase dim als]
cs2,
    [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort (Map Name [TypeBase dim als] -> [Name]
forall k a. Map k a -> [k]
M.keys Map Name [TypeBase dim als]
cs1) [Name] -> [Name] -> Bool
forall a. Eq a => a -> a -> Bool
== [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort (Map Name [TypeBase dim als] -> [Name]
forall k a. Map k a -> [k]
M.keys Map Name [TypeBase dim als]
cs2) =
    Map Name [TypeBase dim als] -> ScalarTypeBase dim als
forall dim as. Map Name [TypeBase dim as] -> ScalarTypeBase dim as
Sum
      (Map Name [TypeBase dim als] -> ScalarTypeBase dim als)
-> Maybe (Map Name [TypeBase dim als])
-> Maybe (ScalarTypeBase dim als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (([TypeBase dim als], [TypeBase dim als])
 -> Maybe [TypeBase dim als])
-> Map Name ([TypeBase dim als], [TypeBase dim als])
-> Maybe (Map Name [TypeBase dim als])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
        (([TypeBase dim als]
 -> [TypeBase dim als] -> Maybe [TypeBase dim als])
-> ([TypeBase dim als], [TypeBase dim als])
-> Maybe [TypeBase dim als]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als))
-> [TypeBase dim als]
-> [TypeBase dim als]
-> Maybe [TypeBase dim als]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM ((Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf)))
        (([TypeBase dim als]
 -> [TypeBase dim als] -> ([TypeBase dim als], [TypeBase dim als]))
-> Map Name [TypeBase dim als]
-> Map Name [TypeBase dim als]
-> Map Name ([TypeBase dim als], [TypeBase dim als])
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
M.intersectionWith (,) Map Name [TypeBase dim als]
cs1 Map Name [TypeBase dim als]
cs2)
unifyScalarTypes Uniqueness -> Uniqueness -> Maybe Uniqueness
_ ScalarTypeBase dim als
_ ScalarTypeBase dim als
_ = Maybe (ScalarTypeBase dim als)
forall a. Maybe a
Nothing

unifyTypeArgs ::
  (ArrayDim dim) =>
  (Uniqueness -> Uniqueness -> Maybe Uniqueness) ->
  TypeArg dim ->
  TypeArg dim ->
  Maybe (TypeArg dim)
unifyTypeArgs :: forall dim.
ArrayDim dim =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeArg dim -> TypeArg dim -> Maybe (TypeArg dim)
unifyTypeArgs Uniqueness -> Uniqueness -> Maybe Uniqueness
_ (TypeArgDim dim
d1 SrcLoc
loc) (TypeArgDim dim
d2 SrcLoc
_) =
  dim -> SrcLoc -> TypeArg dim
forall dim. dim -> SrcLoc -> TypeArg dim
TypeArgDim (dim -> SrcLoc -> TypeArg dim)
-> Maybe dim -> Maybe (SrcLoc -> TypeArg dim)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> dim -> dim -> Maybe dim
forall dim. ArrayDim dim => dim -> dim -> Maybe dim
unifyDims dim
d1 dim
d2 Maybe (SrcLoc -> TypeArg dim)
-> Maybe SrcLoc -> Maybe (TypeArg dim)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SrcLoc -> Maybe SrcLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure SrcLoc
loc
unifyTypeArgs Uniqueness -> Uniqueness -> Maybe Uniqueness
uf (TypeArgType TypeBase dim ()
t1 SrcLoc
loc) (TypeArgType TypeBase dim ()
t2 SrcLoc
_) =
  TypeBase dim () -> SrcLoc -> TypeArg dim
forall dim. TypeBase dim () -> SrcLoc -> TypeArg dim
TypeArgType (TypeBase dim () -> SrcLoc -> TypeArg dim)
-> Maybe (TypeBase dim ()) -> Maybe (SrcLoc -> TypeArg dim)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim () -> TypeBase dim () -> Maybe (TypeBase dim ())
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
uf TypeBase dim ()
t1 TypeBase dim ()
t2 Maybe (SrcLoc -> TypeArg dim)
-> Maybe SrcLoc -> Maybe (TypeArg dim)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SrcLoc -> Maybe SrcLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure SrcLoc
loc
unifyTypeArgs Uniqueness -> Uniqueness -> Maybe Uniqueness
_ TypeArg dim
_ TypeArg dim
_ =
  Maybe (TypeArg dim)
forall a. Maybe a
Nothing

-- | @x \`subtypeOf\` y@ is true if @x@ is a subtype of @y@ (or equal to
-- @y@), meaning @x@ is valid whenever @y@ is.
subtypeOf ::
  ArrayDim dim =>
  TypeBase dim as1 ->
  TypeBase dim as2 ->
  Bool
subtypeOf :: forall dim as1 as2.
ArrayDim dim =>
TypeBase dim as1 -> TypeBase dim as2 -> Bool
subtypeOf TypeBase dim as1
t1 TypeBase dim as2
t2 = Maybe (TypeBase dim ()) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (TypeBase dim ()) -> Bool)
-> Maybe (TypeBase dim ()) -> Bool
forall a b. (a -> b) -> a -> b
$ (Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim () -> TypeBase dim () -> Maybe (TypeBase dim ())
forall als dim.
(Monoid als, ArrayDim dim) =>
(Uniqueness -> Uniqueness -> Maybe Uniqueness)
-> TypeBase dim als -> TypeBase dim als -> Maybe (TypeBase dim als)
unifyTypesU Uniqueness -> Uniqueness -> Maybe Uniqueness
unifyUniqueness (TypeBase dim as1 -> TypeBase dim ()
forall dim as. TypeBase dim as -> TypeBase dim ()
toStruct TypeBase dim as1
t1) (TypeBase dim as2 -> TypeBase dim ()
forall dim as. TypeBase dim as -> TypeBase dim ()
toStruct TypeBase dim as2
t2)
  where
    unifyUniqueness :: Uniqueness -> Uniqueness -> Maybe Uniqueness
unifyUniqueness Uniqueness
u2 Uniqueness
u1 = if Uniqueness
u2 Uniqueness -> Uniqueness -> Bool
`subuniqueOf` Uniqueness
u1 then Uniqueness -> Maybe Uniqueness
forall a. a -> Maybe a
Just Uniqueness
u1 else Maybe Uniqueness
forall a. Maybe a
Nothing

-- | @x `subuniqueOf` y@ is true if @x@ is not less unique than @y@.
subuniqueOf :: Uniqueness -> Uniqueness -> Bool
subuniqueOf :: Uniqueness -> Uniqueness -> Bool
subuniqueOf Uniqueness
Nonunique Uniqueness
Unique = Bool
False
subuniqueOf Uniqueness
_ Uniqueness
_ = Bool
True

-- | Use 'checkTypeExp' to check a type declaration.
checkTypeDecl ::
  MonadTypeChecker m =>
  TypeDeclBase NoInfo Name ->
  m (TypeDeclBase Info VName, Liftedness)
checkTypeDecl :: forall (m :: * -> *).
MonadTypeChecker m =>
TypeDeclBase NoInfo Name -> m (TypeDeclBase Info VName, Liftedness)
checkTypeDecl (TypeDecl TypeExp Name
t NoInfo StructType
NoInfo) = do
  TypeExp Name -> m ()
forall (m :: * -> *). MonadTypeChecker m => TypeExp Name -> m ()
checkForDuplicateNamesInType TypeExp Name
t
  (TypeExp VName
t', StructType
st, Liftedness
l) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t
  (TypeDeclBase Info VName, Liftedness)
-> m (TypeDeclBase Info VName, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeExp VName -> Info StructType -> TypeDeclBase Info VName
forall (f :: * -> *) vn.
TypeExp vn -> f StructType -> TypeDeclBase f vn
TypeDecl TypeExp VName
t' (Info StructType -> TypeDeclBase Info VName)
-> Info StructType -> TypeDeclBase Info VName
forall a b. (a -> b) -> a -> b
$ StructType -> Info StructType
forall a. a -> Info a
Info StructType
st, Liftedness
l)

-- | Type-check a single 'TypeExp', returning the checked 'TypeExp',
-- its fully expanded type (modulo yet-unelaborated type variables),
-- and whether it is potentially higher-order.
checkTypeExp ::
  MonadTypeChecker m =>
  TypeExp Name ->
  m (TypeExp VName, StructType, Liftedness)
checkTypeExp :: forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp (TEVar QualName Name
name SrcLoc
loc) = do
  (QualName VName
name', [TypeParam]
ps, StructType
t, Liftedness
l) <- SrcLoc
-> QualName Name
-> m (QualName VName, [TypeParam], StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
SrcLoc
-> QualName Name
-> m (QualName VName, [TypeParam], StructType, Liftedness)
lookupType SrcLoc
loc QualName Name
name
  case [TypeParam]
ps of
    [] -> (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return (QualName VName -> SrcLoc -> TypeExp VName
forall vn. QualName vn -> SrcLoc -> TypeExp vn
TEVar QualName VName
name' SrcLoc
loc, StructType
t, Liftedness
l)
    [TypeParam]
_ ->
      SrcLoc -> Notes -> Doc -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m (TypeExp VName, StructType, Liftedness))
-> Doc -> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$
        Doc
"Type constructor" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote ([Doc] -> Doc
spread (QualName Name -> Doc
forall a. Pretty a => a -> Doc
ppr QualName Name
name Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: (TypeParam -> Doc) -> [TypeParam] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map TypeParam -> Doc
forall a. Pretty a => a -> Doc
ppr [TypeParam]
ps))
          Doc -> Doc -> Doc
<+> Doc
"used without any arguments."
checkTypeExp (TETuple [TypeExp Name]
ts SrcLoc
loc) = do
  ([TypeExp VName]
ts', [StructType]
ts_s, [Liftedness]
ls) <- [(TypeExp VName, StructType, Liftedness)]
-> ([TypeExp VName], [StructType], [Liftedness])
forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 ([(TypeExp VName, StructType, Liftedness)]
 -> ([TypeExp VName], [StructType], [Liftedness]))
-> m [(TypeExp VName, StructType, Liftedness)]
-> m ([TypeExp VName], [StructType], [Liftedness])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TypeExp Name -> m (TypeExp VName, StructType, Liftedness))
-> [TypeExp Name] -> m [(TypeExp VName, StructType, Liftedness)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp [TypeExp Name]
ts
  (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return ([TypeExp VName] -> SrcLoc -> TypeExp VName
forall vn. [TypeExp vn] -> SrcLoc -> TypeExp vn
TETuple [TypeExp VName]
ts' SrcLoc
loc, [StructType] -> StructType
forall dim as. [TypeBase dim as] -> TypeBase dim as
tupleRecord [StructType]
ts_s, (Liftedness -> Liftedness -> Liftedness)
-> Liftedness -> [Liftedness] -> Liftedness
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Liftedness -> Liftedness -> Liftedness
forall a. Ord a => a -> a -> a
max Liftedness
Unlifted [Liftedness]
ls)
checkTypeExp t :: TypeExp Name
t@(TERecord [(Name, TypeExp Name)]
fs SrcLoc
loc) = do
  -- Check for duplicate field names.
  let field_names :: [Name]
field_names = ((Name, TypeExp Name) -> Name) -> [(Name, TypeExp Name)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, TypeExp Name) -> Name
forall a b. (a, b) -> a
fst [(Name, TypeExp Name)]
fs
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort [Name]
field_names [Name] -> [Name] -> Bool
forall a. Eq a => a -> a -> Bool
== [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort ([Name] -> [Name]
forall a. Ord a => [a] -> [a]
nubOrd [Name]
field_names)) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    SrcLoc -> Notes -> Doc -> m ()
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m ()) -> Doc -> m ()
forall a b. (a -> b) -> a -> b
$ Doc
"Duplicate record fields in" Doc -> Doc -> Doc
<+> TypeExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeExp Name
t Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."

  Map Name (TypeExp VName, StructType, Liftedness)
fs_ts_ls <- (TypeExp Name -> m (TypeExp VName, StructType, Liftedness))
-> Map Name (TypeExp Name)
-> m (Map Name (TypeExp VName, StructType, Liftedness))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp (Map Name (TypeExp Name)
 -> m (Map Name (TypeExp VName, StructType, Liftedness)))
-> Map Name (TypeExp Name)
-> m (Map Name (TypeExp VName, StructType, Liftedness))
forall a b. (a -> b) -> a -> b
$ [(Name, TypeExp Name)] -> Map Name (TypeExp Name)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Name, TypeExp Name)]
fs
  let fs' :: Map Name (TypeExp VName)
fs' = ((TypeExp VName, StructType, Liftedness) -> TypeExp VName)
-> Map Name (TypeExp VName, StructType, Liftedness)
-> Map Name (TypeExp VName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(TypeExp VName
x, StructType
_, Liftedness
_) -> TypeExp VName
x) Map Name (TypeExp VName, StructType, Liftedness)
fs_ts_ls
      ts_s :: Map Name StructType
ts_s = ((TypeExp VName, StructType, Liftedness) -> StructType)
-> Map Name (TypeExp VName, StructType, Liftedness)
-> Map Name StructType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(TypeExp VName
_, StructType
y, Liftedness
_) -> StructType
y) Map Name (TypeExp VName, StructType, Liftedness)
fs_ts_ls
      ls :: Map Name Liftedness
ls = ((TypeExp VName, StructType, Liftedness) -> Liftedness)
-> Map Name (TypeExp VName, StructType, Liftedness)
-> Map Name Liftedness
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(TypeExp VName
_, StructType
_, Liftedness
z) -> Liftedness
z) Map Name (TypeExp VName, StructType, Liftedness)
fs_ts_ls
  (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return
    ( [(Name, TypeExp VName)] -> SrcLoc -> TypeExp VName
forall vn. [(Name, TypeExp vn)] -> SrcLoc -> TypeExp vn
TERecord (Map Name (TypeExp VName) -> [(Name, TypeExp VName)]
forall k a. Map k a -> [(k, a)]
M.toList Map Name (TypeExp VName)
fs') SrcLoc
loc,
      ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) () -> StructType)
-> ScalarTypeBase (DimDecl VName) () -> StructType
forall a b. (a -> b) -> a -> b
$ Map Name StructType -> ScalarTypeBase (DimDecl VName) ()
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
Record Map Name StructType
ts_s,
      (Liftedness -> Liftedness -> Liftedness)
-> Liftedness -> Map Name Liftedness -> Liftedness
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Liftedness -> Liftedness -> Liftedness
forall a. Ord a => a -> a -> a
max Liftedness
Unlifted Map Name Liftedness
ls
    )
checkTypeExp (TEArray TypeExp Name
t DimExp Name
d SrcLoc
loc) = do
  (TypeExp VName
t', StructType
st, Liftedness
l) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t
  (DimExp VName
d', DimDecl VName
d'') <- DimExp Name -> m (DimExp VName, DimDecl VName)
forall {m :: * -> *}.
MonadTypeChecker m =>
DimExp Name -> m (DimExp VName, DimDecl VName)
checkDimExp DimExp Name
d
  case (Liftedness
l, StructType -> ShapeDecl (DimDecl VName) -> Uniqueness -> StructType
forall as dim.
Monoid as =>
TypeBase dim as -> ShapeDecl dim -> Uniqueness -> TypeBase dim as
arrayOf StructType
st ([DimDecl VName] -> ShapeDecl (DimDecl VName)
forall dim. [dim] -> ShapeDecl dim
ShapeDecl [DimDecl VName
d'']) Uniqueness
Nonunique) of
    (Liftedness
Unlifted, StructType
st') -> (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeExp VName -> DimExp VName -> SrcLoc -> TypeExp VName
forall vn. TypeExp vn -> DimExp vn -> SrcLoc -> TypeExp vn
TEArray TypeExp VName
t' DimExp VName
d' SrcLoc
loc, StructType
st', Liftedness
Unlifted)
    (Liftedness
SizeLifted, StructType
_) ->
      SrcLoc -> Notes -> Doc -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m (TypeExp VName, StructType, Liftedness))
-> Doc -> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$
        Doc
"Cannot create array with elements of size-lifted type" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (TypeExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeExp Name
t)
          Doc -> Doc -> Doc
<+/> Doc
"(might cause irregular array)."
    (Liftedness
Lifted, StructType
_) ->
      SrcLoc -> Notes -> Doc -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m (TypeExp VName, StructType, Liftedness))
-> Doc -> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$
        Doc
"Cannot create array with elements of lifted type" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (TypeExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeExp Name
t)
          Doc -> Doc -> Doc
<+/> Doc
"(might contain function)."
  where
    checkDimExp :: DimExp Name -> m (DimExp VName, DimDecl VName)
checkDimExp DimExp Name
DimExpAny =
      (DimExp VName, DimDecl VName) -> m (DimExp VName, DimDecl VName)
forall (m :: * -> *) a. Monad m => a -> m a
return (DimExp VName
forall vn. DimExp vn
DimExpAny, DimDecl VName
forall vn. DimDecl vn
AnyDim)
    checkDimExp (DimExpConst Int
k SrcLoc
dloc) =
      (DimExp VName, DimDecl VName) -> m (DimExp VName, DimDecl VName)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> SrcLoc -> DimExp VName
forall vn. Int -> SrcLoc -> DimExp vn
DimExpConst Int
k SrcLoc
dloc, Int -> DimDecl VName
forall vn. Int -> DimDecl vn
ConstDim Int
k)
    checkDimExp (DimExpNamed QualName Name
v SrcLoc
dloc) = do
      QualName VName
v' <- SrcLoc -> QualName Name -> m (QualName VName)
forall (m :: * -> *).
MonadTypeChecker m =>
SrcLoc -> QualName Name -> m (QualName VName)
checkNamedDim SrcLoc
loc QualName Name
v
      (DimExp VName, DimDecl VName) -> m (DimExp VName, DimDecl VName)
forall (m :: * -> *) a. Monad m => a -> m a
return (QualName VName -> SrcLoc -> DimExp VName
forall vn. QualName vn -> SrcLoc -> DimExp vn
DimExpNamed QualName VName
v' SrcLoc
dloc, QualName VName -> DimDecl VName
forall vn. QualName vn -> DimDecl vn
NamedDim QualName VName
v')
checkTypeExp (TEUnique TypeExp Name
t SrcLoc
loc) = do
  (TypeExp VName
t', StructType
st, Liftedness
l) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (StructType -> Bool
forall {dim} {as}. TypeBase dim as -> Bool
mayContainArray StructType
st) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    SrcLoc -> Doc -> m ()
forall (m :: * -> *) loc.
(MonadTypeChecker m, Located loc) =>
loc -> Doc -> m ()
warn SrcLoc
loc (Doc -> m ()) -> Doc -> m ()
forall a b. (a -> b) -> a -> b
$ Doc
"Declaring" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (StructType -> Doc
forall a. Pretty a => a -> Doc
ppr StructType
st) Doc -> Doc -> Doc
<+> Doc
"as unique has no effect."
  (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeExp VName -> SrcLoc -> TypeExp VName
forall vn. TypeExp vn -> SrcLoc -> TypeExp vn
TEUnique TypeExp VName
t' SrcLoc
loc, StructType
st StructType -> Uniqueness -> StructType
forall dim as. TypeBase dim as -> Uniqueness -> TypeBase dim as
`setUniqueness` Uniqueness
Unique, Liftedness
l)
  where
    mayContainArray :: TypeBase dim as -> Bool
mayContainArray (Scalar Prim {}) = Bool
False
    mayContainArray Array {} = Bool
True
    mayContainArray (Scalar (Record Map Name (TypeBase dim as)
fs)) = (TypeBase dim as -> Bool) -> Map Name (TypeBase dim as) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any TypeBase dim as -> Bool
mayContainArray Map Name (TypeBase dim as)
fs
    mayContainArray (Scalar TypeVar {}) = Bool
True
    mayContainArray (Scalar Arrow {}) = Bool
False
    mayContainArray (Scalar (Sum Map Name [TypeBase dim as]
cs)) = (([TypeBase dim as] -> Bool) -> Map Name [TypeBase dim as] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (([TypeBase dim as] -> Bool) -> Map Name [TypeBase dim as] -> Bool)
-> ((TypeBase dim as -> Bool) -> [TypeBase dim as] -> Bool)
-> (TypeBase dim as -> Bool)
-> Map Name [TypeBase dim as]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase dim as -> Bool) -> [TypeBase dim as] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any) TypeBase dim as -> Bool
mayContainArray Map Name [TypeBase dim as]
cs
checkTypeExp (TEArrow (Just Name
v) TypeExp Name
t1 TypeExp Name
t2 SrcLoc
loc) = do
  (TypeExp VName
t1', StructType
st1, Liftedness
_) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t1
  [(Namespace, Name)]
-> m (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a.
MonadTypeChecker m =>
[(Namespace, Name)] -> m a -> m a
bindSpaced [(Namespace
Term, Name
v)] (m (TypeExp VName, StructType, Liftedness)
 -> m (TypeExp VName, StructType, Liftedness))
-> m (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$ do
    VName
v' <- Namespace -> Name -> SrcLoc -> m VName
forall (m :: * -> *).
MonadTypeChecker m =>
Namespace -> Name -> SrcLoc -> m VName
checkName Namespace
Term Name
v SrcLoc
loc
    VName
-> BoundV
-> m (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a.
MonadTypeChecker m =>
VName -> BoundV -> m a -> m a
bindVal VName
v' ([TypeParam] -> StructType -> BoundV
BoundV [] StructType
st1) (m (TypeExp VName, StructType, Liftedness)
 -> m (TypeExp VName, StructType, Liftedness))
-> m (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$ do
      (TypeExp VName
t2', StructType
st2, Liftedness
_) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t2
      (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( Maybe VName
-> TypeExp VName -> TypeExp VName -> SrcLoc -> TypeExp VName
forall vn.
Maybe vn -> TypeExp vn -> TypeExp vn -> SrcLoc -> TypeExp vn
TEArrow (VName -> Maybe VName
forall a. a -> Maybe a
Just VName
v') TypeExp VName
t1' TypeExp VName
t2' SrcLoc
loc,
          ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) () -> StructType)
-> ScalarTypeBase (DimDecl VName) () -> StructType
forall a b. (a -> b) -> a -> b
$ ()
-> PName
-> StructType
-> StructType
-> ScalarTypeBase (DimDecl VName) ()
forall dim as.
as
-> PName
-> TypeBase dim as
-> TypeBase dim as
-> ScalarTypeBase dim as
Arrow ()
forall a. Monoid a => a
mempty (VName -> PName
Named VName
v') StructType
st1 StructType
st2,
          Liftedness
Lifted
        )
checkTypeExp (TEArrow Maybe Name
Nothing TypeExp Name
t1 TypeExp Name
t2 SrcLoc
loc) = do
  (TypeExp VName
t1', StructType
st1, Liftedness
_) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t1
  (TypeExp VName
t2', StructType
st2, Liftedness
_) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
t2
  (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return
    ( Maybe VName
-> TypeExp VName -> TypeExp VName -> SrcLoc -> TypeExp VName
forall vn.
Maybe vn -> TypeExp vn -> TypeExp vn -> SrcLoc -> TypeExp vn
TEArrow Maybe VName
forall a. Maybe a
Nothing TypeExp VName
t1' TypeExp VName
t2' SrcLoc
loc,
      ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) () -> StructType)
-> ScalarTypeBase (DimDecl VName) () -> StructType
forall a b. (a -> b) -> a -> b
$ ()
-> PName
-> StructType
-> StructType
-> ScalarTypeBase (DimDecl VName) ()
forall dim as.
as
-> PName
-> TypeBase dim as
-> TypeBase dim as
-> ScalarTypeBase dim as
Arrow ()
forall a. Monoid a => a
mempty PName
Unnamed StructType
st1 StructType
st2,
      Liftedness
Lifted
    )
checkTypeExp ote :: TypeExp Name
ote@TEApply {} = do
  (QualName Name
tname, SrcLoc
tname_loc, [TypeArgExp Name]
targs) <- TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
rootAndArgs TypeExp Name
ote
  (QualName VName
tname', [TypeParam]
ps, StructType
t, Liftedness
l) <- SrcLoc
-> QualName Name
-> m (QualName VName, [TypeParam], StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
SrcLoc
-> QualName Name
-> m (QualName VName, [TypeParam], StructType, Liftedness)
lookupType SrcLoc
tloc QualName Name
tname
  if [TypeParam] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeParam]
ps Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [TypeArgExp Name] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeArgExp Name]
targs
    then
      SrcLoc -> Notes -> Doc -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
tloc Notes
forall a. Monoid a => a
mempty (Doc -> m (TypeExp VName, StructType, Liftedness))
-> Doc -> m (TypeExp VName, StructType, Liftedness)
forall a b. (a -> b) -> a -> b
$
        Doc
"Type constructor" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (QualName Name -> Doc
forall a. Pretty a => a -> Doc
ppr QualName Name
tname) Doc -> Doc -> Doc
<+> Doc
"requires" Doc -> Doc -> Doc
<+> Int -> Doc
forall a. Pretty a => a -> Doc
ppr ([TypeParam] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeParam]
ps)
          Doc -> Doc -> Doc
<+> Doc
"arguments, but provided"
          Doc -> Doc -> Doc
<+> Int -> Doc
forall a. Pretty a => a -> Doc
ppr ([TypeArgExp Name] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeArgExp Name]
targs) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."
    else do
      ([TypeArgExp VName]
targs', [Map VName TypeSub]
substs) <- [(TypeArgExp VName, Map VName TypeSub)]
-> ([TypeArgExp VName], [Map VName TypeSub])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(TypeArgExp VName, Map VName TypeSub)]
 -> ([TypeArgExp VName], [Map VName TypeSub]))
-> m [(TypeArgExp VName, Map VName TypeSub)]
-> m ([TypeArgExp VName], [Map VName TypeSub])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TypeParam
 -> TypeArgExp Name -> m (TypeArgExp VName, Map VName TypeSub))
-> [TypeParam]
-> [TypeArgExp Name]
-> m [(TypeArgExp VName, Map VName TypeSub)]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM TypeParam
-> TypeArgExp Name -> m (TypeArgExp VName, Map VName TypeSub)
forall {m :: * -> *} {k}.
(MonadTypeChecker m, Eq k, IsName k) =>
TypeParamBase k
-> TypeArgExp Name -> m (TypeArgExp VName, Map k TypeSub)
checkArgApply [TypeParam]
ps [TypeArgExp Name]
targs
      (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( (TypeExp VName -> TypeArgExp VName -> TypeExp VName)
-> TypeExp VName -> [TypeArgExp VName] -> TypeExp VName
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\TypeExp VName
x TypeArgExp VName
y -> TypeExp VName -> TypeArgExp VName -> SrcLoc -> TypeExp VName
forall vn. TypeExp vn -> TypeArgExp vn -> SrcLoc -> TypeExp vn
TEApply TypeExp VName
x TypeArgExp VName
y SrcLoc
tloc) (QualName VName -> SrcLoc -> TypeExp VName
forall vn. QualName vn -> SrcLoc -> TypeExp vn
TEVar QualName VName
tname' SrcLoc
tname_loc) [TypeArgExp VName]
targs',
          Map VName TypeSub -> StructType -> StructType
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes ([Map VName TypeSub] -> Map VName TypeSub
forall a. Monoid a => [a] -> a
mconcat [Map VName TypeSub]
substs) StructType
t,
          Liftedness
l
        )
  where
    tloc :: SrcLoc
tloc = TypeExp Name -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf TypeExp Name
ote

    rootAndArgs :: MonadTypeChecker m => TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
    rootAndArgs :: forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
rootAndArgs (TEVar QualName Name
qn SrcLoc
loc) = (QualName Name, SrcLoc, [TypeArgExp Name])
-> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall (m :: * -> *) a. Monad m => a -> m a
return (QualName Name
qn, SrcLoc
loc, [])
    rootAndArgs (TEApply TypeExp Name
op TypeArgExp Name
arg SrcLoc
_) = do
      (QualName Name
op', SrcLoc
loc, [TypeArgExp Name]
args) <- TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (QualName Name, SrcLoc, [TypeArgExp Name])
rootAndArgs TypeExp Name
op
      (QualName Name, SrcLoc, [TypeArgExp Name])
-> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall (m :: * -> *) a. Monad m => a -> m a
return (QualName Name
op', SrcLoc
loc, [TypeArgExp Name]
args [TypeArgExp Name] -> [TypeArgExp Name] -> [TypeArgExp Name]
forall a. [a] -> [a] -> [a]
++ [TypeArgExp Name
arg])
    rootAndArgs TypeExp Name
te' =
      SrcLoc
-> Notes -> Doc -> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError (TypeExp Name -> SrcLoc
forall a. Located a => a -> SrcLoc
srclocOf TypeExp Name
te') Notes
forall a. Monoid a => a
mempty (Doc -> m (QualName Name, SrcLoc, [TypeArgExp Name]))
-> Doc -> m (QualName Name, SrcLoc, [TypeArgExp Name])
forall a b. (a -> b) -> a -> b
$
        Doc
"Type" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (TypeExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeExp Name
te') Doc -> Doc -> Doc
<+> Doc
"is not a type constructor."

    checkArgApply :: TypeParamBase k
-> TypeArgExp Name -> m (TypeArgExp VName, Map k TypeSub)
checkArgApply (TypeParamDim k
pv SrcLoc
_) (TypeArgExpDim (DimExpNamed QualName Name
v SrcLoc
dloc) SrcLoc
loc) = do
      QualName VName
v' <- SrcLoc -> QualName Name -> m (QualName VName)
forall (m :: * -> *).
MonadTypeChecker m =>
SrcLoc -> QualName Name -> m (QualName VName)
checkNamedDim SrcLoc
loc QualName Name
v
      (TypeArgExp VName, Map k TypeSub)
-> m (TypeArgExp VName, Map k TypeSub)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( DimExp VName -> SrcLoc -> TypeArgExp VName
forall vn. DimExp vn -> SrcLoc -> TypeArgExp vn
TypeArgExpDim (QualName VName -> SrcLoc -> DimExp VName
forall vn. QualName vn -> SrcLoc -> DimExp vn
DimExpNamed QualName VName
v' SrcLoc
dloc) SrcLoc
loc,
          k -> TypeSub -> Map k TypeSub
forall k a. k -> a -> Map k a
M.singleton k
pv (TypeSub -> Map k TypeSub) -> TypeSub -> Map k TypeSub
forall a b. (a -> b) -> a -> b
$ DimDecl VName -> TypeSub
DimSub (DimDecl VName -> TypeSub) -> DimDecl VName -> TypeSub
forall a b. (a -> b) -> a -> b
$ QualName VName -> DimDecl VName
forall vn. QualName vn -> DimDecl vn
NamedDim QualName VName
v'
        )
    checkArgApply (TypeParamDim k
pv SrcLoc
_) (TypeArgExpDim (DimExpConst Int
x SrcLoc
dloc) SrcLoc
loc) =
      (TypeArgExp VName, Map k TypeSub)
-> m (TypeArgExp VName, Map k TypeSub)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( DimExp VName -> SrcLoc -> TypeArgExp VName
forall vn. DimExp vn -> SrcLoc -> TypeArgExp vn
TypeArgExpDim (Int -> SrcLoc -> DimExp VName
forall vn. Int -> SrcLoc -> DimExp vn
DimExpConst Int
x SrcLoc
dloc) SrcLoc
loc,
          k -> TypeSub -> Map k TypeSub
forall k a. k -> a -> Map k a
M.singleton k
pv (TypeSub -> Map k TypeSub) -> TypeSub -> Map k TypeSub
forall a b. (a -> b) -> a -> b
$ DimDecl VName -> TypeSub
DimSub (DimDecl VName -> TypeSub) -> DimDecl VName -> TypeSub
forall a b. (a -> b) -> a -> b
$ Int -> DimDecl VName
forall vn. Int -> DimDecl vn
ConstDim Int
x
        )
    checkArgApply (TypeParamDim k
pv SrcLoc
_) (TypeArgExpDim DimExp Name
DimExpAny SrcLoc
loc) =
      (TypeArgExp VName, Map k TypeSub)
-> m (TypeArgExp VName, Map k TypeSub)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( DimExp VName -> SrcLoc -> TypeArgExp VName
forall vn. DimExp vn -> SrcLoc -> TypeArgExp vn
TypeArgExpDim DimExp VName
forall vn. DimExp vn
DimExpAny SrcLoc
loc,
          k -> TypeSub -> Map k TypeSub
forall k a. k -> a -> Map k a
M.singleton k
pv (TypeSub -> Map k TypeSub) -> TypeSub -> Map k TypeSub
forall a b. (a -> b) -> a -> b
$ DimDecl VName -> TypeSub
DimSub DimDecl VName
forall vn. DimDecl vn
AnyDim
        )
    checkArgApply (TypeParamType Liftedness
l k
pv SrcLoc
_) (TypeArgExpType TypeExp Name
te) = do
      (TypeExp VName
te', StructType
st, Liftedness
_) <- TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp TypeExp Name
te
      (TypeArgExp VName, Map k TypeSub)
-> m (TypeArgExp VName, Map k TypeSub)
forall (m :: * -> *) a. Monad m => a -> m a
return
        ( TypeExp VName -> TypeArgExp VName
forall vn. TypeExp vn -> TypeArgExp vn
TypeArgExpType TypeExp VName
te',
          k -> TypeSub -> Map k TypeSub
forall k a. k -> a -> Map k a
M.singleton k
pv (TypeSub -> Map k TypeSub) -> TypeSub -> Map k TypeSub
forall a b. (a -> b) -> a -> b
$ TypeBinding -> TypeSub
TypeSub (TypeBinding -> TypeSub) -> TypeBinding -> TypeSub
forall a b. (a -> b) -> a -> b
$ Liftedness -> [TypeParam] -> StructType -> TypeBinding
TypeAbbr Liftedness
l [] StructType
st
        )
    checkArgApply TypeParamBase k
p TypeArgExp Name
a =
      SrcLoc -> Notes -> Doc -> m (TypeArgExp VName, Map k TypeSub)
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
tloc Notes
forall a. Monoid a => a
mempty (Doc -> m (TypeArgExp VName, Map k TypeSub))
-> Doc -> m (TypeArgExp VName, Map k TypeSub)
forall a b. (a -> b) -> a -> b
$
        Doc
"Type argument" Doc -> Doc -> Doc
<+> TypeArgExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeArgExp Name
a
          Doc -> Doc -> Doc
<+> Doc
"not valid for a type parameter"
          Doc -> Doc -> Doc
<+> TypeParamBase k -> Doc
forall a. Pretty a => a -> Doc
ppr TypeParamBase k
p Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."
checkTypeExp t :: TypeExp Name
t@(TESum [(Name, [TypeExp Name])]
cs SrcLoc
loc) = do
  let constructors :: [Name]
constructors = ((Name, [TypeExp Name]) -> Name)
-> [(Name, [TypeExp Name])] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, [TypeExp Name]) -> Name
forall a b. (a, b) -> a
fst [(Name, [TypeExp Name])]
cs
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort [Name]
constructors [Name] -> [Name] -> Bool
forall a. Eq a => a -> a -> Bool
== [Name] -> [Name]
forall a. Ord a => [a] -> [a]
sort ([Name] -> [Name]
forall a. Ord a => [a] -> [a]
nubOrd [Name]
constructors)) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    SrcLoc -> Notes -> Doc -> m ()
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m ()) -> Doc -> m ()
forall a b. (a -> b) -> a -> b
$ Doc
"Duplicate constructors in" Doc -> Doc -> Doc
<+> TypeExp Name -> Doc
forall a. Pretty a => a -> Doc
ppr TypeExp Name
t

  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Name] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Name]
constructors Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
256) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    SrcLoc -> Notes -> Doc -> m ()
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty Doc
"Sum types must have less than 256 constructors."

  Map Name [(TypeExp VName, StructType, Liftedness)]
cs_ts_ls <- (([TypeExp Name] -> m [(TypeExp VName, StructType, Liftedness)])
-> Map Name [TypeExp Name]
-> m (Map Name [(TypeExp VName, StructType, Liftedness)])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (([TypeExp Name] -> m [(TypeExp VName, StructType, Liftedness)])
 -> Map Name [TypeExp Name]
 -> m (Map Name [(TypeExp VName, StructType, Liftedness)]))
-> ((TypeExp Name -> m (TypeExp VName, StructType, Liftedness))
    -> [TypeExp Name] -> m [(TypeExp VName, StructType, Liftedness)])
-> (TypeExp Name -> m (TypeExp VName, StructType, Liftedness))
-> Map Name [TypeExp Name]
-> m (Map Name [(TypeExp VName, StructType, Liftedness)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeExp Name -> m (TypeExp VName, StructType, Liftedness))
-> [TypeExp Name] -> m [(TypeExp VName, StructType, Liftedness)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse) TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *).
MonadTypeChecker m =>
TypeExp Name -> m (TypeExp VName, StructType, Liftedness)
checkTypeExp (Map Name [TypeExp Name]
 -> m (Map Name [(TypeExp VName, StructType, Liftedness)]))
-> Map Name [TypeExp Name]
-> m (Map Name [(TypeExp VName, StructType, Liftedness)])
forall a b. (a -> b) -> a -> b
$ [(Name, [TypeExp Name])] -> Map Name [TypeExp Name]
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Name, [TypeExp Name])]
cs
  let cs' :: Map Name [TypeExp VName]
cs' = (([(TypeExp VName, StructType, Liftedness)] -> [TypeExp VName])
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> Map Name [TypeExp VName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(TypeExp VName, StructType, Liftedness)] -> [TypeExp VName])
 -> Map Name [(TypeExp VName, StructType, Liftedness)]
 -> Map Name [TypeExp VName])
-> (((TypeExp VName, StructType, Liftedness) -> TypeExp VName)
    -> [(TypeExp VName, StructType, Liftedness)] -> [TypeExp VName])
-> ((TypeExp VName, StructType, Liftedness) -> TypeExp VName)
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> Map Name [TypeExp VName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeExp VName, StructType, Liftedness) -> TypeExp VName)
-> [(TypeExp VName, StructType, Liftedness)] -> [TypeExp VName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (\(TypeExp VName
x, StructType
_, Liftedness
_) -> TypeExp VName
x) Map Name [(TypeExp VName, StructType, Liftedness)]
cs_ts_ls
      ts_s :: Map Name [StructType]
ts_s = (([(TypeExp VName, StructType, Liftedness)] -> [StructType])
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> Map Name [StructType]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([(TypeExp VName, StructType, Liftedness)] -> [StructType])
 -> Map Name [(TypeExp VName, StructType, Liftedness)]
 -> Map Name [StructType])
-> (((TypeExp VName, StructType, Liftedness) -> StructType)
    -> [(TypeExp VName, StructType, Liftedness)] -> [StructType])
-> ((TypeExp VName, StructType, Liftedness) -> StructType)
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> Map Name [StructType]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeExp VName, StructType, Liftedness) -> StructType)
-> [(TypeExp VName, StructType, Liftedness)] -> [StructType]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (\(TypeExp VName
_, StructType
y, Liftedness
_) -> StructType
y) Map Name [(TypeExp VName, StructType, Liftedness)]
cs_ts_ls
      ls :: [Liftedness]
ls = (([(TypeExp VName, StructType, Liftedness)] -> [Liftedness])
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> [Liftedness]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (([(TypeExp VName, StructType, Liftedness)] -> [Liftedness])
 -> Map Name [(TypeExp VName, StructType, Liftedness)]
 -> [Liftedness])
-> (((TypeExp VName, StructType, Liftedness) -> Liftedness)
    -> [(TypeExp VName, StructType, Liftedness)] -> [Liftedness])
-> ((TypeExp VName, StructType, Liftedness) -> Liftedness)
-> Map Name [(TypeExp VName, StructType, Liftedness)]
-> [Liftedness]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TypeExp VName, StructType, Liftedness) -> Liftedness)
-> [(TypeExp VName, StructType, Liftedness)] -> [Liftedness]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (\(TypeExp VName
_, StructType
_, Liftedness
z) -> Liftedness
z) Map Name [(TypeExp VName, StructType, Liftedness)]
cs_ts_ls
  (TypeExp VName, StructType, Liftedness)
-> m (TypeExp VName, StructType, Liftedness)
forall (m :: * -> *) a. Monad m => a -> m a
return
    ( [(Name, [TypeExp VName])] -> SrcLoc -> TypeExp VName
forall vn. [(Name, [TypeExp vn])] -> SrcLoc -> TypeExp vn
TESum (Map Name [TypeExp VName] -> [(Name, [TypeExp VName])]
forall k a. Map k a -> [(k, a)]
M.toList Map Name [TypeExp VName]
cs') SrcLoc
loc,
      ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) () -> StructType)
-> ScalarTypeBase (DimDecl VName) () -> StructType
forall a b. (a -> b) -> a -> b
$ Map Name [StructType] -> ScalarTypeBase (DimDecl VName) ()
forall dim as. Map Name [TypeBase dim as] -> ScalarTypeBase dim as
Sum Map Name [StructType]
ts_s,
      (Liftedness -> Liftedness -> Liftedness)
-> Liftedness -> [Liftedness] -> Liftedness
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Liftedness -> Liftedness -> Liftedness
forall a. Ord a => a -> a -> a
max Liftedness
Unlifted [Liftedness]
ls
    )

-- | Check for duplication of names inside a pattern group.  Produces
-- a description of all names used in the pattern group.
checkForDuplicateNames ::
  MonadTypeChecker m =>
  [UncheckedPattern] ->
  m ()
checkForDuplicateNames :: forall (m :: * -> *).
MonadTypeChecker m =>
[UncheckedPattern] -> m ()
checkForDuplicateNames = (StateT (Map Name SrcLoc) m () -> Map Name SrcLoc -> m ()
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
`evalStateT` Map Name SrcLoc
forall a. Monoid a => a
mempty) (StateT (Map Name SrcLoc) m () -> m ())
-> ([UncheckedPattern] -> StateT (Map Name SrcLoc) m ())
-> [UncheckedPattern]
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UncheckedPattern -> StateT (Map Name SrcLoc) m ())
-> [UncheckedPattern] -> StateT (Map Name SrcLoc) m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ UncheckedPattern -> StateT (Map Name SrcLoc) m ()
forall {k} {t :: (* -> *) -> * -> *} {m :: * -> *} {f :: * -> *}.
(MonadState (Map k SrcLoc) (t m), Pretty k, MonadTypeChecker m,
 MonadTrans t, Ord k) =>
PatternBase f k -> t m ()
check
  where
    check :: PatternBase f k -> t m ()
check (Id k
v f PatternType
_ SrcLoc
loc) = k -> SrcLoc -> t m ()
forall {k} {a} {t :: (* -> *) -> * -> *} {m :: * -> *}.
(MonadState (Map k a) (t m), Located a, Pretty k,
 MonadTypeChecker m, MonadTrans t, Ord k) =>
k -> a -> t m ()
seen k
v SrcLoc
loc
    check (PatternParens PatternBase f k
p SrcLoc
_) = PatternBase f k -> t m ()
check PatternBase f k
p
    check Wildcard {} = () -> t m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    check (TuplePattern [PatternBase f k]
ps SrcLoc
_) = (PatternBase f k -> t m ()) -> [PatternBase f k] -> t m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ PatternBase f k -> t m ()
check [PatternBase f k]
ps
    check (RecordPattern [(Name, PatternBase f k)]
fs SrcLoc
_) = ((Name, PatternBase f k) -> t m ())
-> [(Name, PatternBase f k)] -> t m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (PatternBase f k -> t m ()
check (PatternBase f k -> t m ())
-> ((Name, PatternBase f k) -> PatternBase f k)
-> (Name, PatternBase f k)
-> t m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, PatternBase f k) -> PatternBase f k
forall a b. (a, b) -> b
snd) [(Name, PatternBase f k)]
fs
    check (PatternAscription PatternBase f k
p TypeDeclBase f k
_ SrcLoc
_) = PatternBase f k -> t m ()
check PatternBase f k
p
    check PatternLit {} = () -> t m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    check (PatternConstr Name
_ f PatternType
_ [PatternBase f k]
ps SrcLoc
_) = (PatternBase f k -> t m ()) -> [PatternBase f k] -> t m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ PatternBase f k -> t m ()
check [PatternBase f k]
ps

    seen :: k -> a -> t m ()
seen k
v a
loc = do
      Maybe a
already <- (Map k a -> Maybe a) -> t m (Maybe a)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((Map k a -> Maybe a) -> t m (Maybe a))
-> (Map k a -> Maybe a) -> t m (Maybe a)
forall a b. (a -> b) -> a -> b
$ k -> Map k a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup k
v
      case Maybe a
already of
        Just a
prev_loc ->
          m () -> t m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> t m ()) -> m () -> t m ()
forall a b. (a -> b) -> a -> b
$
            a -> Notes -> Doc -> m ()
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError a
loc Notes
forall a. Monoid a => a
mempty (Doc -> m ()) -> Doc -> m ()
forall a b. (a -> b) -> a -> b
$
              Doc
"Name" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (k -> Doc
forall a. Pretty a => a -> Doc
ppr k
v) Doc -> Doc -> Doc
<+> Doc
"also bound at"
                Doc -> Doc -> Doc
<+> String -> Doc
text (a -> String
forall a. Located a => a -> String
locStr a
prev_loc) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."
        Maybe a
Nothing ->
          (Map k a -> Map k a) -> t m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((Map k a -> Map k a) -> t m ()) -> (Map k a -> Map k a) -> t m ()
forall a b. (a -> b) -> a -> b
$ k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert k
v a
loc

-- | Check whether the type contains arrow types that define the same
-- parameter.  These might also exist further down, but that's not
-- really a problem - we mostly do this checking to help the user,
-- since it is likely an error, but it's easy to assign a semantics to
-- it (normal name shadowing).
checkForDuplicateNamesInType ::
  MonadTypeChecker m =>
  TypeExp Name ->
  m ()
checkForDuplicateNamesInType :: forall (m :: * -> *). MonadTypeChecker m => TypeExp Name -> m ()
checkForDuplicateNamesInType = Map Name SrcLoc -> TypeExp Name -> m ()
forall {a} {m :: * -> *}.
(MonadTypeChecker m, Pretty a, Ord a) =>
Map a SrcLoc -> TypeExp a -> m ()
check Map Name SrcLoc
forall a. Monoid a => a
mempty
  where
    check :: Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen (TEArrow (Just a
v) TypeExp a
t1 TypeExp a
t2 SrcLoc
loc)
      | Just SrcLoc
prev_loc <- a -> Map a SrcLoc -> Maybe SrcLoc
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup a
v Map a SrcLoc
seen =
        SrcLoc -> Notes -> Doc -> m ()
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m ()) -> Doc -> m ()
forall a b. (a -> b) -> a -> b
$
          String -> Doc
text String
"Name" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (a -> Doc
forall a. Pretty a => a -> Doc
ppr a
v)
            Doc -> Doc -> Doc
<+> Doc
"also bound at"
            Doc -> Doc -> Doc
<+> String -> Doc
text (SrcLoc -> String
forall a. Located a => a -> String
locStr SrcLoc
prev_loc) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."
      | Bool
otherwise =
        Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen' TypeExp a
t1 m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen' TypeExp a
t2
      where
        seen' :: Map a SrcLoc
seen' = a -> SrcLoc -> Map a SrcLoc -> Map a SrcLoc
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert a
v SrcLoc
loc Map a SrcLoc
seen
    check Map a SrcLoc
seen (TEArrow Maybe a
Nothing TypeExp a
t1 TypeExp a
t2 SrcLoc
_) =
      Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t1 m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t2
    check Map a SrcLoc
seen (TETuple [TypeExp a]
ts SrcLoc
_) = (TypeExp a -> m ()) -> [TypeExp a] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen) [TypeExp a]
ts
    check Map a SrcLoc
seen (TERecord [(Name, TypeExp a)]
fs SrcLoc
_) = ((Name, TypeExp a) -> m ()) -> [(Name, TypeExp a)] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen (TypeExp a -> m ())
-> ((Name, TypeExp a) -> TypeExp a) -> (Name, TypeExp a) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, TypeExp a) -> TypeExp a
forall a b. (a, b) -> b
snd) [(Name, TypeExp a)]
fs
    check Map a SrcLoc
seen (TEUnique TypeExp a
t SrcLoc
_) = Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t
    check Map a SrcLoc
seen (TESum [(Name, [TypeExp a])]
cs SrcLoc
_) = ((Name, [TypeExp a]) -> m [()]) -> [(Name, [TypeExp a])] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((TypeExp a -> m ()) -> [TypeExp a] -> m [()]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen) ([TypeExp a] -> m [()])
-> ((Name, [TypeExp a]) -> [TypeExp a])
-> (Name, [TypeExp a])
-> m [()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Name, [TypeExp a]) -> [TypeExp a]
forall a b. (a, b) -> b
snd) [(Name, [TypeExp a])]
cs
    check Map a SrcLoc
seen (TEApply TypeExp a
t1 (TypeArgExpType TypeExp a
t2) SrcLoc
_) =
      Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t1 m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t2
    check Map a SrcLoc
seen (TEApply TypeExp a
t1 TypeArgExpDim {} SrcLoc
_) =
      Map a SrcLoc -> TypeExp a -> m ()
check Map a SrcLoc
seen TypeExp a
t1
    check Map a SrcLoc
_ TEArray {} = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    check Map a SrcLoc
_ TEVar {} = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | @checkTypeParams ps m@ checks the type parameters @ps@, then
-- invokes the continuation @m@ with the checked parameters, while
-- extending the monadic name map with @ps@.
checkTypeParams ::
  MonadTypeChecker m =>
  [TypeParamBase Name] ->
  ([TypeParamBase VName] -> m a) ->
  m a
checkTypeParams :: forall (m :: * -> *) a.
MonadTypeChecker m =>
[TypeParamBase Name] -> ([TypeParam] -> m a) -> m a
checkTypeParams [TypeParamBase Name]
ps [TypeParam] -> m a
m =
  [(Namespace, Name)] -> m a -> m a
forall (m :: * -> *) a.
MonadTypeChecker m =>
[(Namespace, Name)] -> m a -> m a
bindSpaced ((TypeParamBase Name -> (Namespace, Name))
-> [TypeParamBase Name] -> [(Namespace, Name)]
forall a b. (a -> b) -> [a] -> [b]
map TypeParamBase Name -> (Namespace, Name)
forall {b}. TypeParamBase b -> (Namespace, b)
typeParamSpace [TypeParamBase Name]
ps) (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$
    [TypeParam] -> m a
m ([TypeParam] -> m a) -> m [TypeParam] -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< StateT (Map (Namespace, Name) SrcLoc) m [TypeParam]
-> Map (Namespace, Name) SrcLoc -> m [TypeParam]
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT ((TypeParamBase Name
 -> StateT (Map (Namespace, Name) SrcLoc) m TypeParam)
-> [TypeParamBase Name]
-> StateT (Map (Namespace, Name) SrcLoc) m [TypeParam]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TypeParamBase Name
-> StateT (Map (Namespace, Name) SrcLoc) m TypeParam
forall {t :: (* -> *) -> * -> *} {m :: * -> *}.
(MonadState (Map (Namespace, Name) SrcLoc) (t m),
 MonadTypeChecker m, MonadTrans t) =>
TypeParamBase Name -> t m TypeParam
checkTypeParam [TypeParamBase Name]
ps) Map (Namespace, Name) SrcLoc
forall a. Monoid a => a
mempty
  where
    typeParamSpace :: TypeParamBase b -> (Namespace, b)
typeParamSpace (TypeParamDim b
pv SrcLoc
_) = (Namespace
Term, b
pv)
    typeParamSpace (TypeParamType Liftedness
_ b
pv SrcLoc
_) = (Namespace
Type, b
pv)

    checkParamName :: Namespace -> Name -> SrcLoc -> t m VName
checkParamName Namespace
ns Name
v SrcLoc
loc = do
      Maybe SrcLoc
seen <- (Map (Namespace, Name) SrcLoc -> Maybe SrcLoc)
-> t m (Maybe SrcLoc)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((Map (Namespace, Name) SrcLoc -> Maybe SrcLoc)
 -> t m (Maybe SrcLoc))
-> (Map (Namespace, Name) SrcLoc -> Maybe SrcLoc)
-> t m (Maybe SrcLoc)
forall a b. (a -> b) -> a -> b
$ (Namespace, Name) -> Map (Namespace, Name) SrcLoc -> Maybe SrcLoc
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Namespace
ns, Name
v)
      case Maybe SrcLoc
seen of
        Just SrcLoc
prev ->
          m VName -> t m VName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m VName -> t m VName) -> m VName -> t m VName
forall a b. (a -> b) -> a -> b
$
            SrcLoc -> Notes -> Doc -> m VName
forall (m :: * -> *) loc a.
(MonadTypeChecker m, Located loc) =>
loc -> Notes -> Doc -> m a
typeError SrcLoc
loc Notes
forall a. Monoid a => a
mempty (Doc -> m VName) -> Doc -> m VName
forall a b. (a -> b) -> a -> b
$
              String -> Doc
text String
"Type parameter" Doc -> Doc -> Doc
<+> Doc -> Doc
pquote (Name -> Doc
forall a. Pretty a => a -> Doc
ppr Name
v)
                Doc -> Doc -> Doc
<+> Doc
"previously defined at"
                Doc -> Doc -> Doc
<+> String -> Doc
text (SrcLoc -> String
forall a. Located a => a -> String
locStr SrcLoc
prev) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"."
        Maybe SrcLoc
Nothing -> do
          (Map (Namespace, Name) SrcLoc -> Map (Namespace, Name) SrcLoc)
-> t m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((Map (Namespace, Name) SrcLoc -> Map (Namespace, Name) SrcLoc)
 -> t m ())
-> (Map (Namespace, Name) SrcLoc -> Map (Namespace, Name) SrcLoc)
-> t m ()
forall a b. (a -> b) -> a -> b
$ (Namespace, Name)
-> SrcLoc
-> Map (Namespace, Name) SrcLoc
-> Map (Namespace, Name) SrcLoc
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (Namespace
ns, Name
v) SrcLoc
loc
          m VName -> t m VName
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m VName -> t m VName) -> m VName -> t m VName
forall a b. (a -> b) -> a -> b
$ Namespace -> Name -> SrcLoc -> m VName
forall (m :: * -> *).
MonadTypeChecker m =>
Namespace -> Name -> SrcLoc -> m VName
checkName Namespace
ns Name
v SrcLoc
loc

    checkTypeParam :: TypeParamBase Name -> t m TypeParam
checkTypeParam (TypeParamDim Name
pv SrcLoc
loc) =
      VName -> SrcLoc -> TypeParam
forall vn. vn -> SrcLoc -> TypeParamBase vn
TypeParamDim (VName -> SrcLoc -> TypeParam)
-> t m VName -> t m (SrcLoc -> TypeParam)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Namespace -> Name -> SrcLoc -> t m VName
forall {t :: (* -> *) -> * -> *} {m :: * -> *}.
(MonadState (Map (Namespace, Name) SrcLoc) (t m),
 MonadTypeChecker m, MonadTrans t) =>
Namespace -> Name -> SrcLoc -> t m VName
checkParamName Namespace
Term Name
pv SrcLoc
loc t m (SrcLoc -> TypeParam) -> t m SrcLoc -> t m TypeParam
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SrcLoc -> t m SrcLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure SrcLoc
loc
    checkTypeParam (TypeParamType Liftedness
l Name
pv SrcLoc
loc) =
      Liftedness -> VName -> SrcLoc -> TypeParam
forall vn. Liftedness -> vn -> SrcLoc -> TypeParamBase vn
TypeParamType Liftedness
l (VName -> SrcLoc -> TypeParam)
-> t m VName -> t m (SrcLoc -> TypeParam)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Namespace -> Name -> SrcLoc -> t m VName
forall {t :: (* -> *) -> * -> *} {m :: * -> *}.
(MonadState (Map (Namespace, Name) SrcLoc) (t m),
 MonadTypeChecker m, MonadTrans t) =>
Namespace -> Name -> SrcLoc -> t m VName
checkParamName Namespace
Type Name
pv SrcLoc
loc t m (SrcLoc -> TypeParam) -> t m SrcLoc -> t m TypeParam
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SrcLoc -> t m SrcLoc
forall (f :: * -> *) a. Applicative f => a -> f a
pure SrcLoc
loc

-- | Construct a type argument corresponding to a type parameter.
typeParamToArg :: TypeParam -> StructTypeArg
typeParamToArg :: TypeParam -> StructTypeArg
typeParamToArg (TypeParamDim VName
v SrcLoc
ploc) =
  DimDecl VName -> SrcLoc -> StructTypeArg
forall dim. dim -> SrcLoc -> TypeArg dim
TypeArgDim (QualName VName -> DimDecl VName
forall vn. QualName vn -> DimDecl vn
NamedDim (QualName VName -> DimDecl VName)
-> QualName VName -> DimDecl VName
forall a b. (a -> b) -> a -> b
$ VName -> QualName VName
forall v. v -> QualName v
qualName VName
v) SrcLoc
ploc
typeParamToArg (TypeParamType Liftedness
_ VName
v SrcLoc
ploc) =
  StructType -> SrcLoc -> StructTypeArg
forall dim. TypeBase dim () -> SrcLoc -> TypeArg dim
TypeArgType (ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) () -> StructType)
-> ScalarTypeBase (DimDecl VName) () -> StructType
forall a b. (a -> b) -> a -> b
$ ()
-> Uniqueness
-> TypeName
-> [StructTypeArg]
-> ScalarTypeBase (DimDecl VName) ()
forall dim as.
as
-> Uniqueness -> TypeName -> [TypeArg dim] -> ScalarTypeBase dim as
TypeVar () Uniqueness
Nonunique (VName -> TypeName
typeName VName
v) []) SrcLoc
ploc

-- | A substitution for when using 'substituteTypes'.
data TypeSub
  = TypeSub TypeBinding
  | DimSub (DimDecl VName)
  deriving (Int -> TypeSub -> ShowS
[TypeSub] -> ShowS
TypeSub -> String
(Int -> TypeSub -> ShowS)
-> (TypeSub -> String) -> ([TypeSub] -> ShowS) -> Show TypeSub
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TypeSub] -> ShowS
$cshowList :: [TypeSub] -> ShowS
show :: TypeSub -> String
$cshow :: TypeSub -> String
showsPrec :: Int -> TypeSub -> ShowS
$cshowsPrec :: Int -> TypeSub -> ShowS
Show)

-- | A collection of type substitutions.
type TypeSubs = M.Map VName TypeSub

-- | Apply type substitutions to the given type.
substituteTypes :: Monoid als => TypeSubs -> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes :: forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs TypeBase (DimDecl VName) als
ot = case TypeBase (DimDecl VName) als
ot of
  Array als
als Uniqueness
u ScalarTypeBase (DimDecl VName) ()
at ShapeDecl (DimDecl VName)
shape ->
    TypeBase (DimDecl VName) als
-> ShapeDecl (DimDecl VName)
-> Uniqueness
-> TypeBase (DimDecl VName) als
forall as dim.
Monoid as =>
TypeBase dim as -> ShapeDecl dim -> Uniqueness -> TypeBase dim as
arrayOf
      (Map VName TypeSub -> StructType -> StructType
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs (ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar ScalarTypeBase (DimDecl VName) ()
at) StructType -> als -> TypeBase (DimDecl VName) als
forall dim asf ast. TypeBase dim asf -> ast -> TypeBase dim ast
`setAliases` als
forall a. Monoid a => a
mempty)
      (ShapeDecl (DimDecl VName) -> ShapeDecl (DimDecl VName)
substituteInShape ShapeDecl (DimDecl VName)
shape)
      Uniqueness
u
      TypeBase (DimDecl VName) als
-> (als -> als) -> TypeBase (DimDecl VName) als
forall dim asf ast.
TypeBase dim asf -> (asf -> ast) -> TypeBase dim ast
`addAliases` (als -> als -> als
forall a. Semigroup a => a -> a -> a
<> als
als)
  Scalar (Prim PrimType
t) -> ScalarTypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) als
 -> TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ PrimType -> ScalarTypeBase (DimDecl VName) als
forall dim as. PrimType -> ScalarTypeBase dim as
Prim PrimType
t
  Scalar (TypeVar als
als Uniqueness
u TypeName
v [StructTypeArg]
targs)
    | Just (TypeSub (TypeAbbr Liftedness
_ [TypeParam]
ps StructType
t)) <-
        VName -> Map VName TypeSub -> Maybe TypeSub
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (QualName VName -> VName
forall vn. QualName vn -> vn
qualLeaf (TypeName -> QualName VName
qualNameFromTypeName TypeName
v)) Map VName TypeSub
substs ->
      [TypeParam]
-> TypeBase (DimDecl VName) als
-> [StructTypeArg]
-> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
[TypeParam]
-> TypeBase (DimDecl VName) als
-> [StructTypeArg]
-> TypeBase (DimDecl VName) als
applyType [TypeParam]
ps (StructType
t StructType -> als -> TypeBase (DimDecl VName) als
forall dim asf ast. TypeBase dim asf -> ast -> TypeBase dim ast
`setAliases` als
forall a. Monoid a => a
mempty) ((StructTypeArg -> StructTypeArg)
-> [StructTypeArg] -> [StructTypeArg]
forall a b. (a -> b) -> [a] -> [b]
map StructTypeArg -> StructTypeArg
substituteInTypeArg [StructTypeArg]
targs)
        TypeBase (DimDecl VName) als
-> Uniqueness -> TypeBase (DimDecl VName) als
forall dim as. TypeBase dim as -> Uniqueness -> TypeBase dim as
`setUniqueness` Uniqueness
u TypeBase (DimDecl VName) als
-> (als -> als) -> TypeBase (DimDecl VName) als
forall dim asf ast.
TypeBase dim asf -> (asf -> ast) -> TypeBase dim ast
`addAliases` (als -> als -> als
forall a. Semigroup a => a -> a -> a
<> als
als)
    | Bool
otherwise -> ScalarTypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) als
 -> TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ als
-> Uniqueness
-> TypeName
-> [StructTypeArg]
-> ScalarTypeBase (DimDecl VName) als
forall dim as.
as
-> Uniqueness -> TypeName -> [TypeArg dim] -> ScalarTypeBase dim as
TypeVar als
als Uniqueness
u TypeName
v ([StructTypeArg] -> ScalarTypeBase (DimDecl VName) als)
-> [StructTypeArg] -> ScalarTypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ (StructTypeArg -> StructTypeArg)
-> [StructTypeArg] -> [StructTypeArg]
forall a b. (a -> b) -> [a] -> [b]
map StructTypeArg -> StructTypeArg
substituteInTypeArg [StructTypeArg]
targs
  Scalar (Record Map Name (TypeBase (DimDecl VName) als)
ts) ->
    ScalarTypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) als
 -> TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ Map Name (TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
Record (Map Name (TypeBase (DimDecl VName) als)
 -> ScalarTypeBase (DimDecl VName) als)
-> Map Name (TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ (TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als)
-> Map Name (TypeBase (DimDecl VName) als)
-> Map Name (TypeBase (DimDecl VName) als)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs) Map Name (TypeBase (DimDecl VName) als)
ts
  Scalar (Arrow als
als PName
v TypeBase (DimDecl VName) als
t1 TypeBase (DimDecl VName) als
t2) ->
    ScalarTypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) als
 -> TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ als
-> PName
-> TypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
-> ScalarTypeBase (DimDecl VName) als
forall dim as.
as
-> PName
-> TypeBase dim as
-> TypeBase dim as
-> ScalarTypeBase dim as
Arrow als
als PName
v (Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs TypeBase (DimDecl VName) als
t1) (Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs TypeBase (DimDecl VName) als
t2)
  Scalar (Sum Map Name [TypeBase (DimDecl VName) als]
cs) ->
    ScalarTypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) als
 -> TypeBase (DimDecl VName) als)
-> ScalarTypeBase (DimDecl VName) als
-> TypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ Map Name [TypeBase (DimDecl VName) als]
-> ScalarTypeBase (DimDecl VName) als
forall dim as. Map Name [TypeBase dim as] -> ScalarTypeBase dim as
Sum (Map Name [TypeBase (DimDecl VName) als]
 -> ScalarTypeBase (DimDecl VName) als)
-> Map Name [TypeBase (DimDecl VName) als]
-> ScalarTypeBase (DimDecl VName) als
forall a b. (a -> b) -> a -> b
$ (([TypeBase (DimDecl VName) als] -> [TypeBase (DimDecl VName) als])
-> Map Name [TypeBase (DimDecl VName) als]
-> Map Name [TypeBase (DimDecl VName) als]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([TypeBase (DimDecl VName) als] -> [TypeBase (DimDecl VName) als])
 -> Map Name [TypeBase (DimDecl VName) als]
 -> Map Name [TypeBase (DimDecl VName) als])
-> ((TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als)
    -> [TypeBase (DimDecl VName) als]
    -> [TypeBase (DimDecl VName) als])
-> (TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als)
-> Map Name [TypeBase (DimDecl VName) als]
-> Map Name [TypeBase (DimDecl VName) als]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als)
-> [TypeBase (DimDecl VName) als] -> [TypeBase (DimDecl VName) als]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs) Map Name [TypeBase (DimDecl VName) als]
cs
  where
    substituteInTypeArg :: StructTypeArg -> StructTypeArg
substituteInTypeArg (TypeArgDim DimDecl VName
d SrcLoc
loc) =
      DimDecl VName -> SrcLoc -> StructTypeArg
forall dim. dim -> SrcLoc -> TypeArg dim
TypeArgDim (DimDecl VName -> DimDecl VName
substituteInDim DimDecl VName
d) SrcLoc
loc
    substituteInTypeArg (TypeArgType StructType
t SrcLoc
loc) =
      StructType -> SrcLoc -> StructTypeArg
forall dim. TypeBase dim () -> SrcLoc -> TypeArg dim
TypeArgType (Map VName TypeSub -> StructType -> StructType
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs StructType
t) SrcLoc
loc

    substituteInShape :: ShapeDecl (DimDecl VName) -> ShapeDecl (DimDecl VName)
substituteInShape (ShapeDecl [DimDecl VName]
ds) =
      [DimDecl VName] -> ShapeDecl (DimDecl VName)
forall dim. [dim] -> ShapeDecl dim
ShapeDecl ([DimDecl VName] -> ShapeDecl (DimDecl VName))
-> [DimDecl VName] -> ShapeDecl (DimDecl VName)
forall a b. (a -> b) -> a -> b
$ (DimDecl VName -> DimDecl VName)
-> [DimDecl VName] -> [DimDecl VName]
forall a b. (a -> b) -> [a] -> [b]
map DimDecl VName -> DimDecl VName
substituteInDim [DimDecl VName]
ds

    substituteInDim :: DimDecl VName -> DimDecl VName
substituteInDim (NamedDim QualName VName
v)
      | Just (DimSub DimDecl VName
d) <- VName -> Map VName TypeSub -> Maybe TypeSub
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (QualName VName -> VName
forall vn. QualName vn -> vn
qualLeaf QualName VName
v) Map VName TypeSub
substs = DimDecl VName
d
    substituteInDim DimDecl VName
d = DimDecl VName
d

applyType ::
  Monoid als =>
  [TypeParam] ->
  TypeBase (DimDecl VName) als ->
  [StructTypeArg] ->
  TypeBase (DimDecl VName) als
applyType :: forall als.
Monoid als =>
[TypeParam]
-> TypeBase (DimDecl VName) als
-> [StructTypeArg]
-> TypeBase (DimDecl VName) als
applyType [TypeParam]
ps TypeBase (DimDecl VName) als
t [StructTypeArg]
args =
  Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
forall als.
Monoid als =>
Map VName TypeSub
-> TypeBase (DimDecl VName) als -> TypeBase (DimDecl VName) als
substituteTypes Map VName TypeSub
substs TypeBase (DimDecl VName) als
t
  where
    substs :: Map VName TypeSub
substs = [(VName, TypeSub)] -> Map VName TypeSub
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(VName, TypeSub)] -> Map VName TypeSub)
-> [(VName, TypeSub)] -> Map VName TypeSub
forall a b. (a -> b) -> a -> b
$ (TypeParam -> StructTypeArg -> (VName, TypeSub))
-> [TypeParam] -> [StructTypeArg] -> [(VName, TypeSub)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith TypeParam -> StructTypeArg -> (VName, TypeSub)
forall {a}.
(Eq a, IsName a) =>
TypeParamBase a -> StructTypeArg -> (a, TypeSub)
mkSubst [TypeParam]
ps [StructTypeArg]
args
    -- We are assuming everything has already been type-checked for correctness.
    mkSubst :: TypeParamBase a -> StructTypeArg -> (a, TypeSub)
mkSubst (TypeParamDim a
pv SrcLoc
_) (TypeArgDim (NamedDim QualName VName
v) SrcLoc
_) =
      (a
pv, DimDecl VName -> TypeSub
DimSub (DimDecl VName -> TypeSub) -> DimDecl VName -> TypeSub
forall a b. (a -> b) -> a -> b
$ QualName VName -> DimDecl VName
forall vn. QualName vn -> DimDecl vn
NamedDim QualName VName
v)
    mkSubst (TypeParamDim a
pv SrcLoc
_) (TypeArgDim (ConstDim Int
x) SrcLoc
_) =
      (a
pv, DimDecl VName -> TypeSub
DimSub (DimDecl VName -> TypeSub) -> DimDecl VName -> TypeSub
forall a b. (a -> b) -> a -> b
$ Int -> DimDecl VName
forall vn. Int -> DimDecl vn
ConstDim Int
x)
    mkSubst (TypeParamDim a
pv SrcLoc
_) (TypeArgDim DimDecl VName
AnyDim SrcLoc
_) =
      (a
pv, DimDecl VName -> TypeSub
DimSub DimDecl VName
forall vn. DimDecl vn
AnyDim)
    mkSubst (TypeParamType Liftedness
l a
pv SrcLoc
_) (TypeArgType StructType
at SrcLoc
_) =
      (a
pv, TypeBinding -> TypeSub
TypeSub (TypeBinding -> TypeSub) -> TypeBinding -> TypeSub
forall a b. (a -> b) -> a -> b
$ Liftedness -> [TypeParam] -> StructType -> TypeBinding
TypeAbbr Liftedness
l [] StructType
at)
    mkSubst TypeParamBase a
p StructTypeArg
a =
      String -> (a, TypeSub)
forall a. HasCallStack => String -> a
error (String -> (a, TypeSub)) -> String -> (a, TypeSub)
forall a b. (a -> b) -> a -> b
$ String
"applyType mkSubst: cannot substitute " String -> ShowS
forall a. [a] -> [a] -> [a]
++ StructTypeArg -> String
forall a. Pretty a => a -> String
pretty StructTypeArg
a String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TypeParamBase a -> String
forall a. Pretty a => a -> String
pretty TypeParamBase a
p

-- | A type substituion may be a substitution or a yet-unknown
-- substitution (but which is certainly an overloaded primitive
-- type!).  The latter is used to remove aliases from types that are
-- yet-unknown but that we know cannot carry aliases (see issue #682).
data Subst t = Subst t | PrimSubst | SizeSubst (DimDecl VName)
  deriving (Int -> Subst t -> ShowS
[Subst t] -> ShowS
Subst t -> String
(Int -> Subst t -> ShowS)
-> (Subst t -> String) -> ([Subst t] -> ShowS) -> Show (Subst t)
forall t. Show t => Int -> Subst t -> ShowS
forall t. Show t => [Subst t] -> ShowS
forall t. Show t => Subst t -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Subst t] -> ShowS
$cshowList :: forall t. Show t => [Subst t] -> ShowS
show :: Subst t -> String
$cshow :: forall t. Show t => Subst t -> String
showsPrec :: Int -> Subst t -> ShowS
$cshowsPrec :: forall t. Show t => Int -> Subst t -> ShowS
Show)

instance Functor Subst where
  fmap :: forall a b. (a -> b) -> Subst a -> Subst b
fmap a -> b
f (Subst a
t) = b -> Subst b
forall t. t -> Subst t
Subst (b -> Subst b) -> b -> Subst b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
t
  fmap a -> b
_ Subst a
PrimSubst = Subst b
forall t. Subst t
PrimSubst
  fmap a -> b
_ (SizeSubst DimDecl VName
v) = DimDecl VName -> Subst b
forall t. DimDecl VName -> Subst t
SizeSubst DimDecl VName
v

-- | Class of types which allow for substitution of types with no
-- annotations for type variable names.
class Substitutable a where
  applySubst :: (VName -> Maybe (Subst StructType)) -> a -> a

instance Substitutable (TypeBase (DimDecl VName) ()) where
  applySubst :: (VName -> Maybe (Subst StructType)) -> StructType -> StructType
applySubst = (VName -> Maybe (Subst StructType)) -> StructType -> StructType
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny

instance Substitutable (TypeBase (DimDecl VName) Aliasing) where
  applySubst :: (VName -> Maybe (Subst StructType)) -> PatternType -> PatternType
applySubst = (VName -> Maybe (Subst PatternType)) -> PatternType -> PatternType
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny ((VName -> Maybe (Subst PatternType))
 -> PatternType -> PatternType)
-> ((VName -> Maybe (Subst StructType))
    -> VName -> Maybe (Subst PatternType))
-> (VName -> Maybe (Subst StructType))
-> PatternType
-> PatternType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Subst StructType -> Subst PatternType)
-> Maybe (Subst StructType) -> Maybe (Subst PatternType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((StructType -> PatternType)
-> Subst StructType -> Subst PatternType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StructType -> PatternType
forall dim as. TypeBase dim as -> TypeBase dim Aliasing
fromStruct) (Maybe (Subst StructType) -> Maybe (Subst PatternType))
-> (VName -> Maybe (Subst StructType))
-> VName
-> Maybe (Subst PatternType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.)

instance Substitutable (DimDecl VName) where
  applySubst :: (VName -> Maybe (Subst StructType))
-> DimDecl VName -> DimDecl VName
applySubst VName -> Maybe (Subst StructType)
f (NamedDim (QualName [VName]
_ VName
v))
    | Just (SizeSubst DimDecl VName
d) <- VName -> Maybe (Subst StructType)
f VName
v = DimDecl VName
d
  applySubst VName -> Maybe (Subst StructType)
_ DimDecl VName
d = DimDecl VName
d

instance Substitutable d => Substitutable (ShapeDecl d) where
  applySubst :: (VName -> Maybe (Subst StructType)) -> ShapeDecl d -> ShapeDecl d
applySubst VName -> Maybe (Subst StructType)
f = (d -> d) -> ShapeDecl d -> ShapeDecl d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((d -> d) -> ShapeDecl d -> ShapeDecl d)
-> (d -> d) -> ShapeDecl d -> ShapeDecl d
forall a b. (a -> b) -> a -> b
$ (VName -> Maybe (Subst StructType)) -> d -> d
forall a.
Substitutable a =>
(VName -> Maybe (Subst StructType)) -> a -> a
applySubst VName -> Maybe (Subst StructType)
f

instance Substitutable Pattern where
  applySubst :: (VName -> Maybe (Subst StructType)) -> Pattern -> Pattern
applySubst VName -> Maybe (Subst StructType)
f = Identity Pattern -> Pattern
forall a. Identity a -> a
runIdentity (Identity Pattern -> Pattern)
-> (Pattern -> Identity Pattern) -> Pattern -> Pattern
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASTMapper Identity -> Pattern -> Identity Pattern
forall x (m :: * -> *).
(ASTMappable x, Monad m) =>
ASTMapper m -> x -> m x
astMap ASTMapper Identity
mapper
    where
      mapper :: ASTMapper Identity
mapper =
        ASTMapper :: forall (m :: * -> *).
(ExpBase Info VName -> m (ExpBase Info VName))
-> (VName -> m VName)
-> (QualName VName -> m (QualName VName))
-> (StructType -> m StructType)
-> (PatternType -> m PatternType)
-> ASTMapper m
ASTMapper
          { mapOnExp :: ExpBase Info VName -> Identity (ExpBase Info VName)
mapOnExp = ExpBase Info VName -> Identity (ExpBase Info VName)
forall (m :: * -> *) a. Monad m => a -> m a
return,
            mapOnName :: VName -> Identity VName
mapOnName = VName -> Identity VName
forall (m :: * -> *) a. Monad m => a -> m a
return,
            mapOnQualName :: QualName VName -> Identity (QualName VName)
mapOnQualName = QualName VName -> Identity (QualName VName)
forall (m :: * -> *) a. Monad m => a -> m a
return,
            mapOnStructType :: StructType -> Identity StructType
mapOnStructType = StructType -> Identity StructType
forall (m :: * -> *) a. Monad m => a -> m a
return (StructType -> Identity StructType)
-> (StructType -> StructType) -> StructType -> Identity StructType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> Maybe (Subst StructType)) -> StructType -> StructType
forall a.
Substitutable a =>
(VName -> Maybe (Subst StructType)) -> a -> a
applySubst VName -> Maybe (Subst StructType)
f,
            mapOnPatternType :: PatternType -> Identity PatternType
mapOnPatternType = PatternType -> Identity PatternType
forall (m :: * -> *) a. Monad m => a -> m a
return (PatternType -> Identity PatternType)
-> (PatternType -> PatternType)
-> PatternType
-> Identity PatternType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> Maybe (Subst StructType)) -> PatternType -> PatternType
forall a.
Substitutable a =>
(VName -> Maybe (Subst StructType)) -> a -> a
applySubst VName -> Maybe (Subst StructType)
f
          }

-- | Perform substitutions, from type names to types, on a type. Works
-- regardless of what shape and uniqueness information is attached to the type.
substTypesAny ::
  Monoid as =>
  (VName -> Maybe (Subst (TypeBase (DimDecl VName) as))) ->
  TypeBase (DimDecl VName) as ->
  TypeBase (DimDecl VName) as
substTypesAny :: forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst TypeBase (DimDecl VName) as
ot = case TypeBase (DimDecl VName) as
ot of
  Array as
als Uniqueness
u ScalarTypeBase (DimDecl VName) ()
et ShapeDecl (DimDecl VName)
shape ->
    StructType -> ShapeDecl (DimDecl VName) -> Uniqueness -> StructType
forall as dim.
Monoid as =>
TypeBase dim as -> ShapeDecl dim -> Uniqueness -> TypeBase dim as
arrayOf
      ((VName -> Maybe (Subst StructType)) -> StructType -> StructType
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst StructType)
lookupSubst' (ScalarTypeBase (DimDecl VName) () -> StructType
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar ScalarTypeBase (DimDecl VName) ()
et))
      ((VName -> Maybe (Subst StructType))
-> ShapeDecl (DimDecl VName) -> ShapeDecl (DimDecl VName)
forall a.
Substitutable a =>
(VName -> Maybe (Subst StructType)) -> a -> a
applySubst VName -> Maybe (Subst StructType)
lookupSubst' ShapeDecl (DimDecl VName)
shape)
      Uniqueness
u
      StructType -> as -> TypeBase (DimDecl VName) as
forall dim asf ast. TypeBase dim asf -> ast -> TypeBase dim ast
`setAliases` as
als
  Scalar (Prim PrimType
t) -> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ PrimType -> ScalarTypeBase (DimDecl VName) as
forall dim as. PrimType -> ScalarTypeBase dim as
Prim PrimType
t
  -- We only substitute for a type variable with no arguments, since
  -- type parameters cannot have higher kind.
  Scalar (TypeVar as
als Uniqueness
u TypeName
v [StructTypeArg]
targs) ->
    case VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst (VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
forall a b. (a -> b) -> a -> b
$ QualName VName -> VName
forall vn. QualName vn -> vn
qualLeaf (TypeName -> QualName VName
qualNameFromTypeName TypeName
v) of
      Just (Subst TypeBase (DimDecl VName) as
t) -> (VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst (TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ TypeBase (DimDecl VName) as
t TypeBase (DimDecl VName) as
-> Uniqueness -> TypeBase (DimDecl VName) as
forall dim as. TypeBase dim as -> Uniqueness -> TypeBase dim as
`setUniqueness` Uniqueness
u TypeBase (DimDecl VName) as
-> (as -> as) -> TypeBase (DimDecl VName) as
forall dim asf ast.
TypeBase dim asf -> (asf -> ast) -> TypeBase dim ast
`addAliases` (as -> as -> as
forall a. Semigroup a => a -> a -> a
<> as
als)
      Just Subst (TypeBase (DimDecl VName) as)
PrimSubst -> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ as
-> Uniqueness
-> TypeName
-> [StructTypeArg]
-> ScalarTypeBase (DimDecl VName) as
forall dim as.
as
-> Uniqueness -> TypeName -> [TypeArg dim] -> ScalarTypeBase dim as
TypeVar as
forall a. Monoid a => a
mempty Uniqueness
u TypeName
v ([StructTypeArg] -> ScalarTypeBase (DimDecl VName) as)
-> [StructTypeArg] -> ScalarTypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ (StructTypeArg -> StructTypeArg)
-> [StructTypeArg] -> [StructTypeArg]
forall a b. (a -> b) -> [a] -> [b]
map StructTypeArg -> StructTypeArg
subsTypeArg [StructTypeArg]
targs
      Maybe (Subst (TypeBase (DimDecl VName) as))
_ -> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ as
-> Uniqueness
-> TypeName
-> [StructTypeArg]
-> ScalarTypeBase (DimDecl VName) as
forall dim as.
as
-> Uniqueness -> TypeName -> [TypeArg dim] -> ScalarTypeBase dim as
TypeVar as
als Uniqueness
u TypeName
v ([StructTypeArg] -> ScalarTypeBase (DimDecl VName) as)
-> [StructTypeArg] -> ScalarTypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ (StructTypeArg -> StructTypeArg)
-> [StructTypeArg] -> [StructTypeArg]
forall a b. (a -> b) -> [a] -> [b]
map StructTypeArg -> StructTypeArg
subsTypeArg [StructTypeArg]
targs
  Scalar (Record Map Name (TypeBase (DimDecl VName) as)
ts) -> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ Map Name (TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as
forall dim as. Map Name (TypeBase dim as) -> ScalarTypeBase dim as
Record (Map Name (TypeBase (DimDecl VName) as)
 -> ScalarTypeBase (DimDecl VName) as)
-> Map Name (TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ (TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> Map Name (TypeBase (DimDecl VName) as)
-> Map Name (TypeBase (DimDecl VName) as)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst) Map Name (TypeBase (DimDecl VName) as)
ts
  Scalar (Arrow as
als PName
v TypeBase (DimDecl VName) as
t1 TypeBase (DimDecl VName) as
t2) ->
    ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ as
-> PName
-> TypeBase (DimDecl VName) as
-> TypeBase (DimDecl VName) as
-> ScalarTypeBase (DimDecl VName) as
forall dim as.
as
-> PName
-> TypeBase dim as
-> TypeBase dim as
-> ScalarTypeBase dim as
Arrow as
als PName
v ((VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst TypeBase (DimDecl VName) as
t1) ((VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst TypeBase (DimDecl VName) as
t2)
  Scalar (Sum Map Name [TypeBase (DimDecl VName) as]
ts) ->
    ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall dim as. ScalarTypeBase dim as -> TypeBase dim as
Scalar (ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> ScalarTypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ Map Name [TypeBase (DimDecl VName) as]
-> ScalarTypeBase (DimDecl VName) as
forall dim as. Map Name [TypeBase dim as] -> ScalarTypeBase dim as
Sum (Map Name [TypeBase (DimDecl VName) as]
 -> ScalarTypeBase (DimDecl VName) as)
-> Map Name [TypeBase (DimDecl VName) as]
-> ScalarTypeBase (DimDecl VName) as
forall a b. (a -> b) -> a -> b
$ ([TypeBase (DimDecl VName) as] -> [TypeBase (DimDecl VName) as])
-> Map Name [TypeBase (DimDecl VName) as]
-> Map Name [TypeBase (DimDecl VName) as]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> [TypeBase (DimDecl VName) as] -> [TypeBase (DimDecl VName) as]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
 -> [TypeBase (DimDecl VName) as] -> [TypeBase (DimDecl VName) as])
-> (TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as)
-> [TypeBase (DimDecl VName) as]
-> [TypeBase (DimDecl VName) as]
forall a b. (a -> b) -> a -> b
$ (VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst) Map Name [TypeBase (DimDecl VName) as]
ts
  where
    subsTypeArg :: StructTypeArg -> StructTypeArg
subsTypeArg (TypeArgType StructType
t SrcLoc
loc) =
      StructType -> SrcLoc -> StructTypeArg
forall dim. TypeBase dim () -> SrcLoc -> TypeArg dim
TypeArgType ((VName -> Maybe (Subst StructType)) -> StructType -> StructType
forall as.
Monoid as =>
(VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> TypeBase (DimDecl VName) as -> TypeBase (DimDecl VName) as
substTypesAny VName -> Maybe (Subst StructType)
lookupSubst' StructType
t) SrcLoc
loc
    subsTypeArg (TypeArgDim DimDecl VName
v SrcLoc
loc) =
      DimDecl VName -> SrcLoc -> StructTypeArg
forall dim. dim -> SrcLoc -> TypeArg dim
TypeArgDim ((VName -> Maybe (Subst StructType))
-> DimDecl VName -> DimDecl VName
forall a.
Substitutable a =>
(VName -> Maybe (Subst StructType)) -> a -> a
applySubst VName -> Maybe (Subst StructType)
lookupSubst' DimDecl VName
v) SrcLoc
loc

    lookupSubst' :: VName -> Maybe (Subst StructType)
lookupSubst' = (Subst (TypeBase (DimDecl VName) as) -> Subst StructType)
-> Maybe (Subst (TypeBase (DimDecl VName) as))
-> Maybe (Subst StructType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TypeBase (DimDecl VName) as -> StructType)
-> Subst (TypeBase (DimDecl VName) as) -> Subst StructType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((TypeBase (DimDecl VName) as -> StructType)
 -> Subst (TypeBase (DimDecl VName) as) -> Subst StructType)
-> (TypeBase (DimDecl VName) as -> StructType)
-> Subst (TypeBase (DimDecl VName) as)
-> Subst StructType
forall a b. (a -> b) -> a -> b
$ (as -> ()) -> TypeBase (DimDecl VName) as -> StructType
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (() -> as -> ()
forall a b. a -> b -> a
const ())) (Maybe (Subst (TypeBase (DimDecl VName) as))
 -> Maybe (Subst StructType))
-> (VName -> Maybe (Subst (TypeBase (DimDecl VName) as)))
-> VName
-> Maybe (Subst StructType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VName -> Maybe (Subst (TypeBase (DimDecl VName) as))
lookupSubst