ShotDs.Data.Context (shot_ds v1.0.0)

Copy Markdown View Source

Represents a type environment for parsing and type checking. Can also be created by using the ~e sigil from ShotDs.Hol.Sigils.

Examples

iex> Context.new()
%ShotDs.Data.Context{vars: %{}, consts: %{}, constraints: MapSet.new([])}

iex> Context.new() |> Context.put_var("X", Type.new(:o))
%ShotDs.Data.Context{
  vars: %{"X" => %ShotDs.Data.Type{goal: :o, args: []}},
  consts: %{},
  constraints: MapSet.new([])
}

Summary

Types

t()

The type of the type environment.

Functions

Adds a type constraint to the context.

Returns the type of the given name of a constant or variable. Returns nil if the name is not present in the context.

Creates an empty context.

Associates the constant with the given name with the given type in the context. Overwrites the old value if present.

Associates the variable with the given name with the given type in the context. Overwrites the old value if present.

Types

t()

@type t() :: %ShotDs.Data.Context{
  constraints: MapSet.t({ShotDs.Data.Type.t(), ShotDs.Data.Type.t()}),
  consts: %{required(String.t()) => ShotDs.Data.Type.t()},
  vars: %{required(String.t()) => ShotDs.Data.Type.t()}
}

The type of the type environment.

A context contains the type of variables (:vars) as a Map from its name to its type. Likewise for the constants (:consts). The type constraints are represented as a MapSet of ShotDs.Data.Type pairs.

Functions

add_constraint(ctx, t1, t2)

@spec add_constraint(t(), ShotDs.Data.Type.t(), ShotDs.Data.Type.t()) :: t()

Adds a type constraint to the context.

get_type(ctx, name)

@spec get_type(t(), String.t()) :: ShotDs.Data.Type.t() | nil

Returns the type of the given name of a constant or variable. Returns nil if the name is not present in the context.

new()

@spec new() :: t()

Creates an empty context.

put_const(ctx, name, type)

@spec put_const(t(), String.t(), ShotDs.Data.Type.t()) :: t()

Associates the constant with the given name with the given type in the context. Overwrites the old value if present.

put_var(ctx, name, type)

@spec put_var(t(), String.t(), ShotDs.Data.Type.t()) :: t()

Associates the variable with the given name with the given type in the context. Overwrites the old value if present.