| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Language.Futhark.TypeChecker.Consumption
Contents
Description
Check that a value definition does not violate any consumption constraints, via alias analysis.
This is a very subtle part of the language. The basic idea is "the result of an expression aliases everything that goes into it", but this is in practice too restrictive when we have functions (and polymorphic and higher-order functions!) as well. However, we also do not want to make the user-facing type system too complicated.
Our solution is to infer more precise function types, that directly talk about alias information, via parametricity. These never escape into the user-facing language.
Our overall philosophy is that soundness is (of course) non-negotiable, but simplicity is better than flexibility. Futhark is not a language for very fine-grained reasoning about aliasing.
As an example of the compromise, x |> copy works and produces a fresh
result, whilst id >-> copy loses freshness information.
Further, we only infer freshness for fully saturated higher-order functions, so refactoring to take advantage of partial application may not work. Likewise, we infer only the freshness of the value a function returns, not of what a returned function in turn returns: that must be declared on the returned lambda itself.
Synopsis
- checkValDef :: (QualName VName -> Maybe BoundV) -> (VName, [TypeParam], [Pat ParamType], Exp, ResRetType, Maybe (TypeExp Exp VName), SrcLoc) -> ((Exp, ResRetType), [TypeError])
- data Alias
- = AliasBound Location
- | AliasFree Location
- | AliasSelf
- type Aliases = Set Alias
- type TypeAliases = TypeBase Size Aliases
- inferReturnFreshness :: [Pat ParamType] -> ResType -> TypeAliases -> ResType
Documentation
Arguments
| :: (QualName VName -> Maybe BoundV) | The type scheme of any global, along with the type parameters it is polymorphic in. See Note [Parametric results]. |
| -> (VName, [TypeParam], [Pat ParamType], Exp, ResRetType, Maybe (TypeExp Exp VName), SrcLoc) | |
| -> ((Exp, ResRetType), [TypeError]) |
Type-check a value definition. This also infers a new return type that may be fresher than previously.
For testing
Something a value may share memory with. Every constructor but AliasSelf
denotes a Location. Its variable may be in scope, or be free: either it has
gone out of scope, or it is an internal name standing for an intermediate
value. A free alias behaves more like an equivalence class. See
uniqueness-error18.fut for an example of why this is necessary.
Constructors
| AliasBound Location | |
| AliasFree Location | |
| AliasSelf | Used to represent unknowable internal aliasing, which for example may occur for a function that returns a nonfresh abstract type. (That abstract type may internally be a pair of arrays that alias each other.) |
inferReturnFreshness :: [Pat ParamType] -> ResType -> TypeAliases -> ResType Source #