ShotDs.Data.TypeScheme (shot_ds v1.1.1)

Copy Markdown View Source

Represents a rank-1 polymorphic type scheme:

$$ \forall \alpha_1\dots\alpha_n.\, \tau $$

The quantified variables in vars are type variable references which appear inside the body type and are universally quantified at the outermost level (prenex form).

At each use site, schemes are instantiated into fresh monotypes via instantiate/1, which replaces every quantified variable with a fresh reference. This is the mechanism that allows polymorphic constants like $=^\alpha$ (with scheme $\forall\alpha.\,\alpha\to\alpha\to o$) to be used at different types within the same formula.

Monotypes can be lifted into trivial schemes (with empty vars) using mono/1.

Example

iex> alpha = make_ref()
iex> scheme = TypeScheme.new([alpha], Type.new(alpha, alpha))
iex> match?(%TypeScheme{vars: [^alpha]}, scheme)
true

Summary

Types

t()

Elixir type of a (rank-1 polymorphic) type scheme.

Functions

Returns the set of free type variables in the scheme. This is defined as the set of free type variables in the body without the bound type variables.

Generalizes a given type by binding free type variables in a scheme.

Instantiates the bound type variables in the given type scheme with fresh type variables.

Constructs a trivial type scheme representing the given monotype.

Constructs a type scheme binding the type variables vars in the (possibly polymorphic) type body.

Types

t()

@type t() :: %ShotDs.Data.TypeScheme{
  body: ShotDs.Data.Type.t(),
  vars: [ShotDs.Data.Type.variable_id()]
}

Elixir type of a (rank-1 polymorphic) type scheme.

Functions

free_type_vars(type_scheme)

@spec free_type_vars(t()) :: MapSet.t(ShotDs.Data.Type.variable_id())

Returns the set of free type variables in the scheme. This is defined as the set of free type variables in the body without the bound type variables.

generalize(type, env_vars)

Generalizes a given type by binding free type variables in a scheme.

instantiate(scheme)

@spec instantiate(t()) :: ShotDs.Data.Type.t()

Instantiates the bound type variables in the given type scheme with fresh type variables.

mono(body)

@spec mono(ShotDs.Data.Type.t()) :: t()

Constructs a trivial type scheme representing the given monotype.

new(vars, body)

Constructs a type scheme binding the type variables vars in the (possibly polymorphic) type body.