futhark-0.25.27: An optimising compiler for a functional, array-oriented language.

Safe Haskell | Safe-Inferred
Language | GHC2021

Language.Futhark.TypeChecker.Monad

Description

Main monad in which the type checker runs, as well as ancillary data definitions.

Synopsis

- data TypeM a
- runTypeM :: Env -> ImportTable -> ImportName -> VNameSource -> TypeM a -> (Warnings, Either TypeError (a, VNameSource))
- askEnv :: TypeM Env
- askImportName :: TypeM ImportName
- atTopLevel :: TypeM Bool
- enteringModule :: TypeM a -> TypeM a
- bindSpaced :: [(Namespace, Name, SrcLoc)] -> ([VName] -> TypeM a) -> TypeM a
- bindSpaced1 :: Namespace -> Name -> SrcLoc -> (VName -> TypeM a) -> TypeM a
- bindIdents :: [IdentBase NoInfo VName t] -> TypeM a -> TypeM a
- qualifyTypeVars :: Env -> [VName] -> [VName] -> TypeBase Size as -> TypeBase Size as
- lookupMTy :: SrcLoc -> QualName Name -> TypeM (QualName VName, MTy)
- lookupImport :: SrcLoc -> FilePath -> TypeM (ImportName, Env)
- lookupMod :: SrcLoc -> QualName Name -> TypeM (QualName VName, Mod)
- localEnv :: Env -> TypeM a -> TypeM a
- data TypeError = TypeError Loc Notes (Doc ())
- prettyTypeError :: TypeError -> Doc AnsiStyle
- prettyTypeErrorNoLoc :: TypeError -> Doc AnsiStyle
- withIndexLink :: Doc a -> Doc a -> Doc a
- unappliedFunctor :: MonadTypeChecker m => SrcLoc -> m a
- unknownVariable :: MonadTypeChecker m => Namespace -> QualName Name -> SrcLoc -> m a
- underscoreUse :: MonadTypeChecker m => SrcLoc -> QualName Name -> m a
- data Notes
- aNote :: Doc () -> Notes
- class Monad m => MonadTypeChecker m where
  - warn :: Located loc => loc -> Doc () -> m ()
  - warnings :: Warnings -> m ()
  - newName :: VName -> m VName
  - newID :: Name -> m VName
  - newTypeName :: Name -> m VName
  - bindVal :: VName -> BoundV -> m a -> m a
  - lookupType :: QualName VName -> m ([TypeParam], StructRetType, Liftedness)
  - typeError :: Located loc => loc -> Notes -> Doc () -> m a
- data TypeState
- usedName :: VName -> TypeM ()
- checkName :: Namespace -> Name -> SrcLoc -> TypeM VName
- checkAttr :: MonadTypeChecker m => AttrInfo VName -> m (AttrInfo VName)
- checkQualName :: Namespace -> QualName Name -> SrcLoc -> TypeM (QualName VName)
- checkValName :: QualName Name -> SrcLoc -> TypeM (QualName VName)
- badOnLeft :: Either TypeError a -> TypeM a
- isKnownType :: QualName VName -> TypeM Bool
- module Language.Futhark.Warnings
- data Env = Env {
  - envVtable :: Map VName BoundV
  - envTypeTable :: Map VName TypeBinding
  - envModTypeTable :: Map VName MTy
  - envModTable :: Map VName Mod
  - envNameMap :: NameMap
  }
- type TySet = Map (QualName VName) Liftedness
- data FunModType = FunModType {
  - funModTypeAbs :: TySet
  - funModTypeMod :: Mod
  - funModTypeMty :: MTy
  }
- type ImportTable = Map ImportName Env
- type NameMap = Map (Namespace, Name) (QualName VName)
- data BoundV = BoundV {
  - boundValTParams :: [TypeParam]
  - boundValType :: StructType
  }
- data Mod
  - = ModEnv Env
  - | ModFun FunModType
- data TypeBinding = TypeAbbr Liftedness [TypeParam] StructRetType
- data MTy = MTy {
  - mtyAbs :: TySet
  - mtyMod :: Mod
  }
- anySignedType :: [PrimType]
- anyUnsignedType :: [PrimType]
- anyIntType :: [PrimType]
- anyFloatType :: [PrimType]
- anyNumberType :: [PrimType]
- anyPrimType :: [PrimType]
- data Namespace
  - = Term
  - | Type
  - | Signature
- intrinsicsNameMap :: NameMap
- topLevelNameMap :: NameMap
- mkTypeVarName :: Name -> Int -> Name

# Documentation

data TypeM a Source #

The type checker runs in this monad.

#### Instances

Instances details

Applicative TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods pure :: a -> TypeM a Source # (<*>) :: TypeM (a -> b) -> TypeM a -> TypeM b Source # liftA2 :: (a -> b -> c) -> TypeM a -> TypeM b -> TypeM c Source # (*>) :: TypeM a -> TypeM b -> TypeM b Source # (<*) :: TypeM a -> TypeM b -> TypeM a Source #
Functor TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods fmap :: (a -> b) -> TypeM a -> TypeM b Source # (<$) :: a -> TypeM b -> TypeM a Source #
Monad TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods (>>=) :: TypeM a -> (a -> TypeM b) -> TypeM b Source # (>>) :: TypeM a -> TypeM b -> TypeM b Source # return :: a -> TypeM a Source #
MonadTypeChecker TypeM Source # |

Instance details

Defined in Language.Futhark.TypeChecker.Monad

Methods

warn :: Located loc => loc -> Doc () -> TypeM () Source #

warnings :: Warnings -> TypeM () Source #

newName :: VName -> TypeM VName Source #

newID :: Name -> TypeM VName Source #

newTypeName :: Name -> TypeM VName Source #

bindVal :: VName -> BoundV -> TypeM a -> TypeM a Source #

lookupType :: QualName VName -> TypeM ([TypeParam], StructRetType, Liftedness) Source #

typeError :: Located loc => loc -> Notes -> Doc () -> TypeM a Source #

MonadError TypeError TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods throwError :: TypeError -> TypeM a Source # catchError :: TypeM a -> (TypeError -> TypeM a) -> TypeM a Source #
MonadState TypeState TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods get :: TypeM TypeState Source # put :: TypeState -> TypeM () Source # state :: (TypeState -> (a, TypeState)) -> TypeM a Source #

runTypeM :: Env -> ImportTable -> ImportName -> VNameSource -> TypeM a -> (Warnings, Either TypeError (a, VNameSource)) Source #

Run a `TypeM` computation.

askEnv :: TypeM Env Source #

Retrieve the current `Env`.

askImportName :: TypeM ImportName Source #

The name of the current file/import.

atTopLevel :: TypeM Bool Source #

Are we type-checking at the top level, or are we inside a nested module?

enteringModule :: TypeM a -> TypeM a Source #

We are now going to type-check the body of a module.

bindSpaced :: [(Namespace, Name, SrcLoc)] -> ([VName] -> TypeM a) -> TypeM a Source #

Map source-level names to fresh unique internal names, and evaluate a type checker context with the mapping active.

bindSpaced1 :: Namespace -> Name -> SrcLoc -> (VName -> TypeM a) -> TypeM a Source #

Map single source-level name to fresh unique internal names, and evaluate a type checker context with the mapping active.

bindIdents :: [IdentBase NoInfo VName t] -> TypeM a -> TypeM a Source #

Bind these identifiers in the name map and also check whether they have been used.

qualifyTypeVars :: Env -> [VName] -> [VName] -> TypeBase Size as -> TypeBase Size as Source #

Try to prepend qualifiers to the type names such that they represent how to access the type in some scope.

lookupMTy :: SrcLoc -> QualName Name -> TypeM (QualName VName, MTy) Source #

Look up a module type.

lookupImport :: SrcLoc -> FilePath -> TypeM (ImportName, Env) Source #

Look up an import.

lookupMod :: SrcLoc -> QualName Name -> TypeM (QualName VName, Mod) Source #

localEnv :: Env -> TypeM a -> TypeM a Source #

Evaluate a `TypeM` computation within an extended (not replaced) environment.

data TypeError Source #

Information about an error during type checking.

Constructors

TypeError Loc Notes (Doc ()) |

#### Instances

Instances details

MonadError TypeError TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods throwError :: TypeError -> TypeM a Source # catchError :: TypeM a -> (TypeError -> TypeM a) -> TypeM a Source #
MonadError TypeError TermTypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Terms.Monad Methods throwError :: TypeError -> TermTypeM a Source # catchError :: TermTypeM a -> (TypeError -> TermTypeM a) -> TermTypeM a Source #

prettyTypeError :: TypeError -> Doc AnsiStyle Source #

Prettyprint type error.

prettyTypeErrorNoLoc :: TypeError -> Doc AnsiStyle Source #

Prettyprint type error, without location information. This can be used for cases where the location is printed in some other way.

withIndexLink :: Doc a -> Doc a -> Doc a Source #

Attach a reference to documentation explaining the error in more detail.

unappliedFunctor :: MonadTypeChecker m => SrcLoc -> m a Source #

An unexpected functor appeared!

unknownVariable :: MonadTypeChecker m => Namespace -> QualName Name -> SrcLoc -> m a Source #

An unknown variable was referenced.

underscoreUse :: MonadTypeChecker m => SrcLoc -> QualName Name -> m a Source #

A name prefixed with an underscore was used.

data Notes Source #

A collection of extra information regarding a type error.

#### Instances

Instances details

Monoid Notes Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods mempty :: Notes Source # mappend :: Notes -> Notes -> Notes Source # mconcat :: [Notes] -> Notes Source #
Semigroup Notes Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods (<>) :: Notes -> Notes -> Notes Source # sconcat :: NonEmpty Notes -> Notes Source # stimes :: Integral b => b -> Notes -> Notes Source #
Pretty Notes Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods pretty :: Notes -> Doc ann Source # prettyList :: [Notes] -> Doc ann Source #

aNote :: Doc () -> Notes Source #

A single note.

class Monad m => MonadTypeChecker m where Source #

Monads that support type checking. The reason we have this internal interface is because we use distinct monads for checking expressions and declarations.

Minimal complete definition

warn, warnings, newName, newTypeName, bindVal, lookupType, typeError

Methods

warn :: Located loc => loc -> Doc () -> m () Source #

warnings :: Warnings -> m () Source #

newName :: VName -> m VName Source #

newID :: Name -> m VName Source #

newTypeName :: Name -> m VName Source #

bindVal :: VName -> BoundV -> m a -> m a Source #

lookupType :: QualName VName -> m ([TypeParam], StructRetType, Liftedness) Source #

typeError :: Located loc => loc -> Notes -> Doc () -> m a Source #

#### Instances

Instances details

MonadTypeChecker TypeM Source # |

Instance details

Defined in Language.Futhark.TypeChecker.Monad

Methods

warn :: Located loc => loc -> Doc () -> TypeM () Source #

warnings :: Warnings -> TypeM () Source #

newName :: VName -> TypeM VName Source #

newID :: Name -> TypeM VName Source #

newTypeName :: Name -> TypeM VName Source #

bindVal :: VName -> BoundV -> TypeM a -> TypeM a Source #

lookupType :: QualName VName -> TypeM ([TypeParam], StructRetType, Liftedness) Source #

typeError :: Located loc => loc -> Notes -> Doc () -> TypeM a Source #

MonadTypeChecker TermTypeM Source # |

Instance details

Defined in Language.Futhark.TypeChecker.Terms.Monad

Methods

warn :: Located loc => loc -> Doc () -> TermTypeM () Source #

warnings :: Warnings -> TermTypeM () Source #

newName :: VName -> TermTypeM VName Source #

newID :: Name -> TermTypeM VName Source #

newTypeName :: Name -> TermTypeM VName Source #

bindVal :: VName -> BoundV -> TermTypeM a -> TermTypeM a Source #

lookupType :: QualName VName -> TermTypeM ([TypeParam], StructRetType, Liftedness) Source #

typeError :: Located loc => loc -> Notes -> Doc () -> TermTypeM a Source #

data TypeState Source #

#### Instances

Instances details

MonadState TypeState TypeM Source # |
Instance details Defined in Language.Futhark.TypeChecker.Monad Methods get :: TypeM TypeState Source # put :: TypeState -> TypeM () Source # state :: (TypeState -> (a, TypeState)) -> TypeM a Source #

usedName :: VName -> TypeM () Source #

Indicate that this name has been used. This is usually done implicitly by other operations, but sometimes we want to make a "fake" use to avoid things like top level functions being considered unused.

checkName :: Namespace -> Name -> SrcLoc -> TypeM VName Source #

Elaborate the given name in the given namespace at the given location, producing the corresponding unique `VName`.

checkAttr :: MonadTypeChecker m => AttrInfo VName -> m (AttrInfo VName) Source #

Type-check an attribute.

checkQualName :: Namespace -> QualName Name -> SrcLoc -> TypeM (QualName VName) Source #

Elaborate the given qualified name in the given namespace at the given location, producing the corresponding unique `QualName`.

checkValName :: QualName Name -> SrcLoc -> TypeM (QualName VName) Source #

Elaborate the given qualified name in the given namespace at the given location, producing the corresponding unique `QualName`. Fails if the name is a module.

badOnLeft :: Either TypeError a -> TypeM a Source #

Turn a `Left` `TypeError` into an actual error.

isKnownType :: QualName VName -> TypeM Bool Source #

Does a type with this name already exist? This is used for warnings, so it is OK it's a little unprincipled.

module Language.Futhark.Warnings

data Env Source #

Modules produces environment with this representation.

Constructors

Env |

Fields

- envVtable :: Map VName BoundV
- envTypeTable :: Map VName TypeBinding
- envModTypeTable :: Map VName MTy
- envModTable :: Map VName Mod
- envNameMap :: NameMap

#### Instances

Instances details

Monoid Env Source # |
Instance details Defined in Language.Futhark.Semantic Methods mempty :: Env Source # mappend :: Env -> Env -> Env Source # mconcat :: [Env] -> Env Source #
Semigroup Env Source # |
Instance details Defined in Language.Futhark.Semantic Methods (<>) :: Env -> Env -> Env Source # sconcat :: NonEmpty Env -> Env Source # stimes :: Integral b => b -> Env -> Env Source #
Show Env Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> Env -> ShowS Source # show :: Env -> String Source # showList :: [Env] -> ShowS Source #
Pretty Env Source # |
Instance details Defined in Language.Futhark.Semantic Methods pretty :: Env -> Doc ann Source # prettyList :: [Env] -> Doc ann Source #

type TySet = Map (QualName VName) Liftedness Source #

A mapping of abstract types to their liftedness.

data FunModType Source #

A parametric functor consists of a set of abstract types, the environment of its parameter, and the resulting module type.

Constructors

FunModType |

Fields

- funModTypeAbs :: TySet
- funModTypeMod :: Mod
- funModTypeMty :: MTy

#### Instances

Instances details

Show FunModType Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> FunModType -> ShowS Source # show :: FunModType -> String Source # showList :: [FunModType] -> ShowS Source #

type ImportTable = Map ImportName Env Source #

A mapping from import import names to `Env`s. This is used to resolve `import` declarations.

type NameMap = Map (Namespace, Name) (QualName VName) Source #

A mapping from names (which always exist in some namespace) to a unique (tagged) name.

data BoundV Source #

Type parameters, list of parameter types (optinally named), and return type. The type parameters are in scope in both parameter types and the return type. Non-functional values have only a return type.

Constructors

BoundV |

Fields

- boundValTParams :: [TypeParam]
- boundValType :: StructType

#### Instances

Instances details

Show BoundV Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> BoundV -> ShowS Source # show :: BoundV -> String Source # showList :: [BoundV] -> ShowS Source #

data Mod Source #

Representation of a module, which is either a plain environment, or a parametric module ("functor" in SML).

Constructors

ModEnv Env |
ModFun FunModType |

#### Instances

Instances details

Show Mod Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> Mod -> ShowS Source # show :: Mod -> String Source # showList :: [Mod] -> ShowS Source #
Pretty Mod Source # |
Instance details Defined in Language.Futhark.Semantic Methods pretty :: Mod -> Doc ann Source # prettyList :: [Mod] -> Doc ann Source #

data TypeBinding Source #

A binding from a name to its definition as a type. We allow a return type here to support type abbreviations that hide some inner sizes (these must necessarily be `Lifted` or `SizeLifted`).

Constructors

TypeAbbr Liftedness [TypeParam] StructRetType |

#### Instances

Instances details

Show TypeBinding Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> TypeBinding -> ShowS Source # show :: TypeBinding -> String Source # showList :: [TypeBinding] -> ShowS Source #
Eq TypeBinding Source # |
Instance details Defined in Language.Futhark.Semantic Methods (==) :: TypeBinding -> TypeBinding -> Bool Source # (/=) :: TypeBinding -> TypeBinding -> Bool Source #

data MTy Source #

Representation of a module type.

Constructors

MTy |

Fields

- mtyAbs :: TySet

  Abstract types in the module type.

- mtyMod :: Mod

#### Instances

Instances details

Show MTy Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> MTy -> ShowS Source # show :: MTy -> String Source # showList :: [MTy] -> ShowS Source #
Pretty MTy Source # |
Instance details Defined in Language.Futhark.Semantic Methods pretty :: MTy -> Doc ann Source # prettyList :: [MTy] -> Doc ann Source #

anySignedType :: [PrimType] Source #

All signed integer types.

anyUnsignedType :: [PrimType] Source #

All unsigned integer types.

anyIntType :: [PrimType] Source #

All integer types.

anyFloatType :: [PrimType] Source #

All floating-point types.

anyNumberType :: [PrimType] Source #

All number types.

anyPrimType :: [PrimType] Source #

All primitive types.

data Namespace Source #

The space inhabited by a name.

Constructors

Term | Functions and values.
Type |
Signature |

#### Instances

Instances details

Enum Namespace Source # |

Instance details

Defined in Language.Futhark.Semantic

Methods

succ :: Namespace -> Namespace Source #

pred :: Namespace -> Namespace Source #

toEnum :: Int -> Namespace Source #

fromEnum :: Namespace -> Int Source #

enumFrom :: Namespace -> [Namespace] Source #

enumFromThen :: Namespace -> Namespace -> [Namespace] Source #

enumFromTo :: Namespace -> Namespace -> [Namespace] Source #

enumFromThenTo :: Namespace -> Namespace -> Namespace -> [Namespace] Source #

Show Namespace Source # |
Instance details Defined in Language.Futhark.Semantic Methods showsPrec :: Int -> Namespace -> ShowS Source # show :: Namespace -> String Source # showList :: [Namespace] -> ShowS Source #
Eq Namespace Source # |
Instance details Defined in Language.Futhark.Semantic Methods (==) :: Namespace -> Namespace -> Bool Source # (/=) :: Namespace -> Namespace -> Bool Source #
Ord Namespace Source # |

Instance details

Defined in Language.Futhark.Semantic

Methods

compare :: Namespace -> Namespace -> Ordering Source #

(<) :: Namespace -> Namespace -> Bool Source #

(<=) :: Namespace -> Namespace -> Bool Source #

(>) :: Namespace -> Namespace -> Bool Source #

(>=) :: Namespace -> Namespace -> Bool Source #

max :: Namespace -> Namespace -> Namespace Source #

min :: Namespace -> Namespace -> Namespace Source #

Pretty Namespace Source # |
Instance details Defined in Language.Futhark.Semantic Methods pretty :: Namespace -> Doc ann Source # prettyList :: [Namespace] -> Doc ann Source #

intrinsicsNameMap :: NameMap Source #

The `NameMap` corresponding to the intrinsics module.

topLevelNameMap :: NameMap Source #

The names that are available in the initial environment.

mkTypeVarName :: Name -> Int -> Name Source #

Construct the name of a new type variable given a base description and a tag number (note that this is distinct from actually constructing a VName; the tag here is intended for human consumption but the machine does not care).